拆分包含多个分隔符的字符串

时间:2016-04-18 07:36:58

标签: python string split

我需要拆分以下字符串:

str1 = "0020 04,5",3

预期输出为:

['0020 04,5', '3'] 

我试过的是

print [p for p in re.split('(|".+?"|,|)', a) if p.strip()]

2 个答案:

答案 0 :(得分:1)

您可以将字符串转换为列表:

str1 = ['"0020 04,5",3']

并使用csv模块:

>>> from csv import reader
>>> for line in reader(str1):
...     print line
... 
['0020 04,5', '3']

答案 1 :(得分:0)

a=[]

for x in str:

   a.append(x)

print a

['0020 04,5', 3]

另一种方式

[a.append(x) for x in str]