split csv file with comma

Asked by judy johnson

I need to parse out a csv file but within a line, but there may be a comma within a field that throws off the parsing

if the line = "tom,jones,123 street apt 6", then line.split(',') parses correctly with 3 elements but

if the line = "tom,jones,123 street, apt 6", then the line.split(',' parses correctly with 4 elements but I need "123 street, apt 6"

I have tried putting double quotes around the address, single quotes around the address, \ after street. I have tried using split(',',3). I've tried using rsplit(',',3).

How do I deal with a comma within a field?

Question information

Language:
English Edit question
Status:
Answered
For:
Python Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Selene ToyKeeper (toykeeper) said :
#2

Try using Python's "csv" module instead of parsing it yourself.

Can you help with this problem?

Provide an answer of your own, or ask judy johnson for more information if necessary.

To post a message you must log in.