Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string, ignoring delimiter within quotation marks (python)

Tags:

python

csv

I would like to split a string on a comma, but ignore cases when it is within quotation marks:

for example:

teststring = '48, "one, two", "2011/11/03"'
teststring.split(",")
['48', ' "one', ' two"', ' "2011/11/03"']

and the output I would like is:

['48', ' "one, two"', ' "2011/11/03"']

Is this possible?

like image 658
djmac Avatar asked Nov 21 '11 07:11

djmac


People also ask

How do you split a quote in Python?

Python3. here split() method will split the string for every quotation ( " ) .

How do you keep quotation marks in Python?

' You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.

What does .split mean in Python?

Definition and Usage The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


2 Answers

The csv module will work if you set options to handle this dialect:

>>> import csv
>>> teststring = '48, "one, two", "2011/11/03"'
>>> for line in csv.reader([teststring], skipinitialspace=True):
    print line


['48', 'one, two', '2011/11/03']
like image 168
Raymond Hettinger Avatar answered Sep 25 '22 07:09

Raymond Hettinger


You can use the csv module from the standard library:

>>> import csv
>>> testdata = ['48, "one, two", "2011/11/03"']
>>> testcsv = csv.reader(testdata,skipinitialspace=True)
>>> testcsv.next()
['48', 'one, two', '2011/11/03']

The one thing to watch out for is that the csv.reader objects expect an iterator which will return a string each time next() is called. This means that you can't pass a string string straight to a reader(), but you can enclose it in a list as above.

You'll have to be careful with the format of your data or tell csv how to handle it. By default the quotes have to come immediately after the comma or the csv module will interpret the field as beginning with a space rather than being quoted. You can fix this using the skipinitialspace option.

like image 22
Dave Webb Avatar answered Sep 23 '22 07:09

Dave Webb