Reputation: 2989
I want to separate a string based on comma, but when the string is within double quotes the commas should be kept as it is. For that I wrote the following code. However, the code given below does not seem to work. Can someone please help me figure out as to what the error is?
>>> from csv import reader
>>> l='k,<livesIn> "Dayton,_Ohio"'
>>> l1=[]
>>> l1.append(l)
>>> for line1 in reader(l1):
print line1
The output which I am getting is:
['k', '<livesIn> "Dayton', '_Ohio"']
Whereas I want the output as: ['k', '<livesIn> "Dayton,_Ohio"']
i.e. I don't want "Dayton,_Ohio"
to get separated.
Upvotes: 3
Views: 290
Reputation: 6972
So here is a way.
>>> from csv import reader
>>> l='k,<livesIn> "Dayton,_Ohio"'
>>> l1=[]
>>> l1.append(l)
>>> for line in reader(l1):
... print list((line[0], ','.join(line[1:])))
...
['k', '<livesIn> "Dayton,_Ohio"']
Upvotes: 1