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?
Python3. here split() method will split the string for every quotation ( " ) .
' 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.
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.
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']
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With