具有以下内容:
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/