python argparse 传递分隔列表

标签 python argparse

具有以下内容:

parser.add_argument("-l", "--library", type=str, nargs="*", dest="scanLibrary")

在某些情况下,传递的名称列表可能包含空格。 argparse 在空白处分解列表,所以

mything.py -l test of foo, game of bar, How I foo bar your mother

网我:

scanLibrary=['test', 'of', 'foo,', 'game', 'of', 'bar,', 'How', 'I', 'foo', 'bar', 'your', 'mother']

那么我如何让 argeparse 使用我选择的分隔符?

更新: 根据 Martijn Pieters 的建议,我做了以下更改:

parser.add_argument("-l", "--library", type=str, nargs="*", dest="scanLibrary")
print args.scanLibrary
print args.scanLibrary[0].split(',')

结果是:

mything.py -l "test of foo, game of bar, How I foo bar your mother"
['test of foo, game of bar, How I foo bar your mother']
['test of foo', ' game of bar', ' How I foo bar your mother']

我可以很容易地清理前导空间。谢谢

最佳答案

你不能。 shell 在这里进行解析;它将参数作为解析列表传递给进程。

为了防止这种情况,请引用你的论点:

mything.py -l "test of foo, game of bar, How I foo bar your mother"

关于python argparse 传递分隔列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24000888/

相关文章:

python - 在 pandas python 中按列计算匹配部分字符串的出现次数

python - AMQP 中的消息路由

python - pygtk 居中对话框按钮

python - 在组的子组中使用 pandas 中的模式来替换缺失值

python - 退格键\n\t 在长字符串中不起作用 python argparse

python - 如何将 argparse 值存储在变量中?

python - 如何在 Python 3 中进行 URL 编码?

python - Argparse 字典到命名空间

python - 如何使用 argparse Python 使用 '=' 作为参数分隔符?

python-2.7 - 使用 Class() 时如何显示 argparse 中所有子解析器的帮助