creating a list from a inconsistent text file

J

Jetus

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 "


I would like to pick out data in a particular column. So am I right in
thinking that I would need to covert each line into a list, then
choose the appropriate column via a index?

How do I convert that line into a list?
 
M

Marc 'BlackJack' Rintsch

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
 
J

Jetus

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.


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

Mike Kent

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?

Marc has already given you the correct answer. You really should read
up on the csv module.
 
D

Diez B. Roggisch

Jetus said:
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
 
K

Kam-Hung Soh

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top