[英]Python Split string by comma unless proceeded by a space
我有一个长字符串,我需要用逗号分隔成一个列表,但是当逗号后面跟着一个空格时,我不希望它被分隔。 例如:
细绳:
str = 'foo,bar,hello, you'
所需列表:
des_list = ['foo', 'bar', 'hello, you']
提前致谢
您可以使用re
模块:
import re
txt = 'foo,bar,hello, you'
print( re.split(r',(?=[^\s]+)', txt) )
印刷:
['foo', 'bar', 'hello, you']
使用“拆分()” function:
str = 'foo,bar,hello, you'
des_list = str.split(',')
#des_list = ['foo', 'bar', 'hello, you']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.