Python 从命令行接受文件并将参数传递给函数不会给出任何输出
我正在编写一个脚本来接受(可选)命令行中的两个参数:--top按计数返回最前面的单词,例如--top 5,返回top 5--lower可在计算唯一值之前降低单词列表 到了这个阶段,我没有得到任何输出:Python 从命令行接受文件并将参数传递给函数不会给出任何输出,python,command-line-arguments,argparse,Python,Command Line Arguments,Argparse,我正在编写一个脚本来接受(可选)命令行中的两个参数:--top按计数返回最前面的单词,例如--top 5,返回top 5--lower可在计算唯一值之前降低单词列表 到了这个阶段,我没有得到任何输出: import collections import argparse def counts(text, top = 10, case = None): """ returns counts. Default is top 10 frequent words without change
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.('StackOverFlow' and 'stackoverflow' 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.('StackOverFlow' and 'stackoverflow' 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))
如果您使用的是ArgParse,请正确执行此操作并使其处理所有参数。不要使用
sys.argv
查看此问题以获得有关使用argparse:parse_args
parsessys.argv[1://code>的文件参数的帮助。如果它首先遇到“text.txt”字符串,我希望它会引发一个未知值
错误。该错误似乎是由open
命令使用“open('--top')产生的。这也让它看起来像你没有包括'text.txt'或你把它放在最后。谢谢你的评论,我根据建议修复了帖子。但是脚本仍然没有给出任何输出。@feijao这是因为您对--lower
使用了默认的存储
;在这种情况下,它需要任何值,例如--下1
。您还可以使用actionstore\u true
,然后可以省略--lower
的值。在这种情况下,您必须更改count()
中的条件,因为args.lower
只能是True
或False
。哦,还有丢失的输出。。。你不打印任何东西<代码>打印(计数(text=file,top=args.top,case=args.lower))
您还可以添加到--top
的定义中。这样您就不必担心调用计数时args.top
是否为None
)