简体   繁体   English

用逗号分割字符串,忽略字符串内的逗号。 正在尝试CSV

[英]Split string by comma, ignoring comma inside string. Am trying CSV

I have a string like this: 我有一个像这样的字符串:

s = '1,2,"hello, there"'

And I want to turn it into a list: 我想把它变成一个列表:

[1,2,"hello, there"]

Normally I'd use split: 通常我会使用split:

my_list = s.split(",") 

However, that doesn't work if there's a comma in a string. 但是,如果字符串中有逗号,则无法使用。

So, I've read that I need to use cvs, but I don't really see how. 因此,我读到我需要使用cvs,但我真的不知道如何使用。 I've tried: 我试过了:

from csv import reader
s = '1,2,"hello, there"'
ll = reader(s)
print ll 
for row in ll:
    print row

Which writes: 其中写道:

<_csv.reader object at 0x020EBC70>

['1']
['', '']
['2']
['', '']
['hello, there']

I've also tried with 我也尝试过

ll = reader(s, delimiter=',')

It is that way because you provide the csv reader input as a string. 之所以这样,是因为您将csv阅读器输入作为字符串提供。 If you do not want to use a file or a StringIO object just wrap your string in a list as shown below. 如果您不想使用文件或StringIO对象,只需将字符串包装在列表中,如下所示。

>>> import csv
>>> s = ['1,2,"hello, there"']
>>> ll = csv.reader(s, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
>>> list(ll)
[['1', '2', 'hello, there']]

It sounds like you probably want to use the csv module. 听起来您可能要使用csv模块。 To use the reader on a string, you want a StringIO object. 要在字符串上使用阅读器,您需要一个StringIO对象。

As an example: 举个例子:

>> import csv, StringIO
>> print list(csv.reader(StringIO.StringIO(s)))
[['1', '2', 'hello, there']]

To clarify, csv.reader expects a buffer object, not a string. 为了明确csv.readercsv.reader需要一个缓冲区对象,而不是字符串。 So StringIO does the trick. 因此StringIO做到了。 However, if you're reading this csv from a file object, (a typical use case) you can just as easily give the file object to the reader and it'll work the same way. 但是,如果您要从文件对象读取csv(一种典型的用例),则可以轻松地将文件对象提供给读取器,并且它将以相同的方式工作。

It's usually easier to re-use than to invent a bicycle... You just to use csv library properly. 通常,重用比发明自行车更容易。您只是正确使用csv If you can't for some reason, you can always check the source code out and learn how's the parsing done there. 如果由于某种原因不能执行此操作,则始终可以将源代码签出,并了解其中的解析方式。

Example for parsing a single string into a list. 将单个字符串解析为列表的示例。 Notice that the string in wrapped in list. 请注意,该字符串包含在列表中。

>>> import csv
>>> s = '1,2,"hello, there"'
>>> list(csv.reader([s]))[0]
['1', '2', 'hello, there']

You can split first by the string delimiters, then by the commas for every even index (The ones not in the string) 您可以先按字符串定界符进行分隔,然后按逗号分隔每个偶数索引(不在字符串中的那些)

import itertools

new_data = s.split('"')
for i in range(len(new_data)):
    if i % 2 == 1: # Skip odd indices, making them arrays
       new_data[i] = [new_data[i]]
    else:
        new_data[i] = new_data[i].split(",")
data = itertools.chain(*new_data)

Which goes something like: 像这样:

'1,2,"hello, there"'
['1,2,', 'hello, there']
[['1', '2'], ['hello, there']]
['1', '2', 'hello, there']

But it's probably better to use the csv library if that's what you're working with. 但是,如果要使用的是csv库,则最好使用csv库。

You could also use ast.literal_eval if you want to preserve the integers: 如果要保留整数,也可以使用ast.literal_eval

>>> from ast import literal_eval
>>> literal_eval('[{}]'.format('1,2,"hello, there"'))
[1, 2, 'hello, there']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM