python argparse传递分隔的lis

2024-09-30 04:39:21 发布

您现在位置:Python中文网/ 问答频道 /正文

包括以下内容:

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

在某些情况下,传递的名称列表可能包含空格。argparse正在将列表拆分为空白,因此

^{pr2}$

网罗我:

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']

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


Tags: oftestaddgameparseryourfootype
2条回答

不能评论你的问题,因为50次重复。我只想说你可以使用:

'   some string '.strip()

获得:

^{pr2}$

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

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

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

相关问题 更多 >

    热门问题