从命令行接受文件并将参数传递给函数没有输出
Accept file and pass arguments to function from command line gives no output
我正在编写一个脚本来接受(可选)命令行中的两个参数:--top to return top words by count e.g. --top 5, returns top 5; --lower 在计算唯一值之前降低单词列表。
我到了这个阶段,但没有得到任何输出:
import collections
import argparse
def counts(text, top = 10, case = None):
""" returns counts. Default is top 10 frequent words without change of case"""
# split on whitespace
word_list = text.split()
if case is None:
c = collections.Counter(word_list)
return c.most_common(top)
else:
c = collections.Counter([w.lower() for w in word_list])
return c.most_common(top)
# declare parser
parser = argparse.ArgumentParser()
# add argument --top
parser.add_argument("--top", help="returns top N words. If not specified it returns top 10", type=int)
# add argument --lower
parser.add_argument("--lower", help = "lowercase all the words.('Whosebug' and 'Whosebug' are counted equally.")
# add argument filename
parser.add_argument("filename", help = "accepts txt file")
args = parser.parse_args()
# read text file
file = open(args.filename, 'r').read()
counts(text = file, top = args.top, case = args.lower)
当我运行脚本与
$python script.py text.txt --top 5 --lower
我没有输出。我哪里出错了?
如果文件要输出一些东西,我希望:
(word1 count1)
(word2 count2)
(word3 count3)
(word4 count4)
(word5 count5)
根据上面惊人的评论,工作代码是:
import collections
import argparse
def counts(text, top = 10, case = False):
""" returns counts. Default is top 10 frequent words without change of case"""
# split on whitespace
word_list = text.split()
if case is False:
c = collections.Counter(word_list)
return c.most_common(top)
else:
c = collections.Counter([w.lower() for w in word_list])
return c.most_common(top)
# declare parser
parser = argparse.ArgumentParser()
# add argument --top
parser.add_argument("--top", help="returns top N words. If not specified it returns top 10", type=int)
# add argument --lower
parser.add_argument("--lower", help = "lowercase all the words.('Whosebug' and 'Whosebug' are counted equally.",action='store_true')
# add argument filename
parser.add_argument("filename", help = "accepts txt file")
args = parser.parse_args()
# read text file
file = open(args.filename, 'r').read()
if args.top:
print(counts(text = file, top = args.top, case = args.lower))
else:
print(counts(text = file, case = args.lower))
我正在编写一个脚本来接受(可选)命令行中的两个参数:--top to return top words by count e.g. --top 5, returns top 5; --lower 在计算唯一值之前降低单词列表。
我到了这个阶段,但没有得到任何输出:
import collections
import argparse
def counts(text, top = 10, case = None):
""" returns counts. Default is top 10 frequent words without change of case"""
# split on whitespace
word_list = text.split()
if case is None:
c = collections.Counter(word_list)
return c.most_common(top)
else:
c = collections.Counter([w.lower() for w in word_list])
return c.most_common(top)
# declare parser
parser = argparse.ArgumentParser()
# add argument --top
parser.add_argument("--top", help="returns top N words. If not specified it returns top 10", type=int)
# add argument --lower
parser.add_argument("--lower", help = "lowercase all the words.('Whosebug' and 'Whosebug' are counted equally.")
# add argument filename
parser.add_argument("filename", help = "accepts txt file")
args = parser.parse_args()
# read text file
file = open(args.filename, 'r').read()
counts(text = file, top = args.top, case = args.lower)
当我运行脚本与
$python script.py text.txt --top 5 --lower
我没有输出。我哪里出错了?
如果文件要输出一些东西,我希望:
(word1 count1)
(word2 count2)
(word3 count3)
(word4 count4)
(word5 count5)
根据上面惊人的评论,工作代码是:
import collections
import argparse
def counts(text, top = 10, case = False):
""" returns counts. Default is top 10 frequent words without change of case"""
# split on whitespace
word_list = text.split()
if case is False:
c = collections.Counter(word_list)
return c.most_common(top)
else:
c = collections.Counter([w.lower() for w in word_list])
return c.most_common(top)
# declare parser
parser = argparse.ArgumentParser()
# add argument --top
parser.add_argument("--top", help="returns top N words. If not specified it returns top 10", type=int)
# add argument --lower
parser.add_argument("--lower", help = "lowercase all the words.('Whosebug' and 'Whosebug' are counted equally.",action='store_true')
# add argument filename
parser.add_argument("filename", help = "accepts txt file")
args = parser.parse_args()
# read text file
file = open(args.filename, 'r').read()
if args.top:
print(counts(text = file, top = args.top, case = args.lower))
else:
print(counts(text = file, case = args.lower))