creating a list from a inconsistent text file

Kam-Hung Soh kamhung.soh at gmail.com
Fri May 2 17:13:46 EDT 2008


On Fri, 02 May 2008 23:47:11 +1000, Jetus <stevegill7 at gmail.com> wrote:

> 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?

The reader in the csv package automatically handles double-quoted fields.

-- 
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>



More information about the Python-list mailing list