如何将逗号分隔的字符串解析为列表(caveat)?

如何将逗号分隔的字符串解析为列表(caveat)?,第1张

如何将逗号分隔的字符串解析为列表(caveat)?

shlex模块解决方案允许转义引号,一个引号的转义和所有花哨的东西外壳程序支持。

>>> import shlex>>> my_splitter = shlex.shlex('''foo, bar, "one, two", three four''', posix=True)>>> my_splitter.whitespace += ','>>> my_splitter.whitespace_split = True>>> print list(my_splitter)['foo', 'bar', 'one, two', 'three', 'four']

转义引号示例:

>>> my_splitter = shlex.shlex('''"test, a",'foo,bar",baz',bar xc3xa4 baz''',        posix=True) >>> my_splitter.whitespace = ',' ; my_splitter.whitespace_split = True >>> print list(my_splitter)['test, a', 'foo,bar",baz', 'bar xc3xa4 baz']


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5462359.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存