creating a list from a inconsistent text file

Diez B. Roggisch deets at nospam.web.de
Fri May 2 10:45:56 EDT 2008


Jetus schrieb:
> On May 2, 7:19 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>> On Fri, 02 May 2008 04:14:47 -0700, Jetus wrote:
>>> I have a comma delimited file that is separated by comma's, and then
>>> sometimes by ","
>>> c:\temp\05-06-08\Sale1,659 CECIL,"659 CECIL,40211",
>>> 1,659,CECIL,AVENUE,LOUISVILLE,40211,"$65,276.78 "
>>> c:\temp\05-06-08\Sale2,637 SOUTH 27TH,"637 SOUTH 27TH,40211",
>>> 2,637,SOUTH 27TH,STREET,LOUISVILLE,40211,"$45,456.95 "
>>> c:\temp\05-06-08\Sale3,2709 ELLIOT,"2709 ELLIOT,40211",
>>> 3,2709,ELLIOT,AVENUE,LOUISVILLE,40211,"$49,349.66 "
>> The items are always delimited by commas but some items themselves contain
>> a comma and therefore are enclosed in double quotes.  So it's not
>> inconsistent.
>>
>>> How do I convert that line into a list?
>> Use the `csv` module in the standard library.
>>
>> Ciao,
>>         Marc 'BlackJack' Rintsch
> 
> Hello Marc;
> Thanks for the input! I am worried about the comma in the "" data
> items, how do I tell Python to look for the "" data first, then use
> the comma separator?

As Marc said: use the CSV-module. It will ignore the comma inside 
""-surrounded fields. Then, once you extracted the columns, you might 
consider using the split-method on the columns to process further.

Diez



More information about the Python-list mailing list