By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

creating a list from a inconsistent text file

P: n/a
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?
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jun 27 '08 #2

P: n/a
On May 2, 7:19 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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?
Jun 27 '08 #3

P: n/a
On May 2, 9:47 am, Jetus <stevegi...@gmail.comwrote:
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.
Jun 27 '08 #4

P: n/a
Jetus schrieb:
On May 2, 7:19 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>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.7 8 "
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.6 6 "
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
Jun 27 '08 #5

P: n/a
On Fri, 02 May 2008 23:47:11 +1000, Jetus <st********@gmail.comwrote:
On May 2, 7:19 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>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>
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.