【问题标题】:Accept file and pass arguments to function from command line gives no output接受文件并将参数从命令行传递给函数没有输出
【发布时间】:2019-07-05 16:47:13
【问题描述】:

我正在编写一个脚本来接受(可选)命令行中的两个参数:--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)

【问题讨论】:

  • 如果您使用 ArgParse,请正确执行此操作并使其处理您的所有参数。不要使用sys.argv
  • 查看此问题以获取有关使用 argparse 文件参数的帮助:stackoverflow.com/questions/18862836/…
  • parse_args 解析 sys.argv[1:]。如果它首先遇到'text.txt'字符串,我希望它会引发unknown value 错误。该错误似乎是由open 命令使用`open('--top') 产生的。这也使您看起来好像没有包含“text.txt”或者您把它放在最后。
  • 感谢 cmets,我根据建议修复了帖子。但是脚本仍然没有给出任何输出。
  • @feijao 那是因为你使用默认的action store--lower;在这种情况下它需要任何值,例如--lower 1。您也可以使用操作store_true,然后您可以省略--lower 的值。在这种情况下,您必须更改count() 中的条件,因为args.lower 只能是TrueFalse。哦,还有缺少的输出……你什么都不打印。 print(counts(text = file, top = args.top, case = args.lower))

标签: python command-line-arguments argparse


【解决方案1】:

基于上面令人惊叹的 cmets,工作代码是:

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

【讨论】:

  • 您也可以将default=10 添加到您对--top 的定义中。那么你在调用counts 时就不必担心args.topNone ;)
猜你喜欢
  • 2012-08-13
  • 1970-01-01
  • 2015-04-17
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多