Alex Rivera | Logout

How do I split a line by commas, but ignore commas within quotes

Asked 2011-10-07T02:42:19.110
17

Possible Duplicate:
How to read a CSV line with "?

I have seen a number of related questions but none have directly addressed what I am trying to do. I am reading in lines of text from a CSV file.

All the items are in quotes and some have additional commas within the quotes. I would like to split the line along commas, but ignore the commas within quotes. Is there a way to do this within Python that does not require a number of regex statements.

An example is:

"114111","Planes,Trains,and Automobiles","50","BOOK"

which I would like parsed into 4 separate variables of values:

"114111"  "Planes,Trains,and Automobiles"  "50" "Book"

Is there a simple option in line.split() that I am missing ?

Edit
Report

1 Answer

-7

You can probably split on "," that is "[quote][comma][quote]"

the other option is coming up with an escape character, so if somebody wants to embed a comma in the string they do \c and if they want a backslash they do \\. Then you have to split the string, then unescape it before processing.

answered 2011-10-07T02:45:59.030

Your Answer