[英]Transform comma separated string into a list but ignore comma in quotes
How do I convert "1,,2'3,4'"
into a list? 如何将
"1,,2'3,4'"
转换为列表? Commas separate the individual items, unless they are within quotes. 逗号分隔各个项目,除非它们在引号内。 In that case, the comma is to be included in the item.
在这种情况下,逗号将包含在项目中。
This is the desired result: ['1', '', '2', '3,4']
. 这是期望的结果:
['1', '', '2', '3,4']
。 One regex I found on another thread to ignore the quotes is as follows: 我在另一个线程上发现忽略引号的一个正则表达式如下:
re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
But this gives me this output: 但这给了我这个输出:
['', '1', ',,', "2'3,4'", '']
I can't understand, where these extra empty strings are coming from, and why the two commas are even being printed at all, let alone together. 我无法理解,这些额外的空字符串来自哪里,为什么两个逗号甚至都被打印出来,更不用说在一起了。
I tried making this regex myself: 我自己尝试制作这个正则表达式:
re.compile(r'''(, | "[^"]*" | '[^']*')''')
which ended up not detecting anything, and just returned my original list. 最终没有检测到任何东西,只是返回原来的清单。
I don't understand why, shouldn't it detect the commas at the very least? 我不明白为什么,它至少不应该检测到逗号? The same problem occurs if I add a
?
如果我添加一个
?
after the comma. 在逗号之后。
Instead of a regular expression, you might be better off using the csv
module since what you are dealing with is a CSV string: 您可能最好使用
csv
模块而不是正则表达式,因为您处理的是CSV字符串:
from cStringIO import StringIO
from csv import reader
file_like_object = StringIO("1,,2,'3,4'")
csv_reader = reader(file_like_object, quotechar="'")
for row in csv_reader:
print row
This results in the following output: 这导致以下输出:
['1', '', '2', '3,4']
pyparsing includes a predefined expression for comma-separated lists: pyparsing包含逗号分隔列表的预定义表达式:
>>> from pyparsing import commaSeparatedList
>>> s = "1,,2'3,4'"
>>> print commaSeparatedList.parseString(s).asList()
['1', '', "2'3", "4'"]
Hmm, looks like you have a typo in your data, missing a comma after the 2: 嗯,看起来你的数据中有一个拼写错误,2之后缺少逗号:
>>> s = "1,,2,'3,4'"
>>> print commaSeparatedList.parseString(s).asList()
['1', '', '2', "'3,4'"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.