简体   繁体   English

将逗号分隔的字符串转换为列表但忽略引号中的逗号

[英]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.

相关问题 Python字符串列表到1个字符串,用逗号和引号(,&“)分隔 - Python list of strings to 1 string separated by comma & quotes (, & ") 将列表转换为包含双引号并用逗号分隔的单个字符串 - convert a list into a single string consisting with double quotes and separated by comma 加入字符串列表,使每个字符串在引号内并以逗号分隔 - Join a list of strings such that each string is within quotes and comma separated 将列表转换成逗号分隔并在python中添加引号 - Converting a list into comma separated and add quotes in python 在逗号分隔列表中搜索字符串 - Searching for string in comma separated list 如何避免使用逗号分隔的字符串从python中的列表中加入引号 - How to avoid quotes from a comma separated string joined from a list in python 如何将逗号分隔的字符串转换为用引号''括起来的每个单词到 python 中的列表? - How to convert a comma separated string with each word enclosed in quotes ' ' to a list in python? 如何将逗号分隔的字符串用引号括起来,用空格转换为带有单独元素的列表 - How to convert comma separated string enclosed in quotes, with spaces to a list with separate elements 如何将逗号分隔的字符串转换为 Python 中的项目中包含逗号的列表? - How to convert comma separated string to list that contains comma in items in Python? 将逗号分隔的浮点数转换为列表? - Convert comma separated string of floats into list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM