python – 使用shlex拆分多行字符串并保留引号字符

如何使用Python的shlex拆分字符串,同时保留shlex拆分的引号字符?

样本输入:

Two Words
"A Multi-line
 comment."

期望的输出:

['Two', 'Words', '"A Multi-line\ncomment."']

请注意包装多行字符串的双引号.我通读了shlex documentation,但我没有看到明显的选择.这需要正则表达式解决方案吗?

解决方法:

>>> print(s)
Two Words
"A Multi-line
 comment."
>>> shlex.split(s)
['Two', 'Words', 'A Multi-line\n comment.']
>>> shlex.split(s, posix=False)
['Two', 'Words', '"A Multi-line\n comment."']
>>> 

Changed in version 2.6: Added the posix parameter.

上一篇:如何使用内联变量创建多行Python字符串?


下一篇:c# – WinForms ListBox控件上的多行列表项?