接受文件并将参数从命令行传递给函数不会产生任何输出

时间:2019-02-12 05:21:28

标签: python command-line-arguments argparse

我正在编写一个脚本来接受(可选)命令行中的两个参数:--top通过计数返回顶部单词,例如--top 5,返回前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.('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)

1 个答案:

答案 0 :(得分:0)

基于上面的惊人注释,工作代码为:

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))