为什么python argparse模块不解析哈希标记?

时间:2016-01-10 18:49:41

标签: python argparse

我需要从sys.argv获取一个主题标签列表。我使用argparse模块:

parser = argparse.ArgumentParser()
parser.add_argument('-hashtag', nargs='*')

但是当我启动这样的脚本时:

python filename.py -hashtags #one #two #three

我明白了     Namespace(hashtag=[])

如果我将#替换为*,我通常会Namespace(hashtag=['*one', '*two', '*three'])。如何使用哈希标志?

1 个答案:

答案 0 :(得分:4)

在shell脚本中,#符号通常会开始发表评论。使用\前缀它们。

python filename.py -hashtags \#one \#two \#three