471,337 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

delineating by comma where commas inside quotation marks don't count

I want to open a text file for reading and delineate it by comma. I also
want any data
surrounded by quotation marks that has a comma in it, not to count the
commas inside the
quotation marks

if the file testfile.txt contains the following;

5,Tuesday,"May is a spring month",Father's Day
1,Saturday,"June,July and August",Independance Day
and I write the following program

fname = open('testfile.txt', 'r')
ct=1
while ct != 3:
rd=(fname.readline())
filedata=rd.split(',')
print "var2=",filedata[2]
ct +=1

fname.close

filedata[2] will = "May is a spring month"; on the first pass and

filedata[2] will = "June; on the second pass

How do I get filedata[2] to = "June,July and August" on the second pass

and filedata[3] to = Independance Day
Oct 25 '07 #1
3 2118
On Oct 24, 8:56 pm, "Junior" <mlon...@verizon.netwrote:
I want to open a text file for reading and delineate it by comma. I also
want any data
surrounded by quotation marks that has a comma in it, not to count the
commas inside the
quotation marks
Use the csv module.

Oct 25 '07 #2
On Oct 25, 3:56 am, "Junior" <mlon...@verizon.netwrote:
I want to open a text file for reading and delineate it by comma. I also
want any data
surrounded by quotation marks that has a comma in it, not to count the
commas inside the
quotation marks

if the file testfile.txt contains the following;

5,Tuesday,"May is a spring month",Father's Day
1,Saturday,"June,July and August",Independance Day

and I write the following program

fname = open('testfile.txt', 'r')
ct=1
while ct != 3:
rd=(fname.readline())
filedata=rd.split(',')
print "var2=",filedata[2]
ct +=1

fname.close

filedata[2] will = "May is a spring month"; on the first pass and

filedata[2] will = "June; on the second pass

How do I get filedata[2] to = "June,July and August" on the second pass

and filedata[3] to = Independance Day
why not using csv?
but u would better put all strings into ""
else u can

r = open('file').read().splitlines()

for i in r:
l1 = i.split(',',2)
l2 = l1.pop().rsplit(',',1)
print l1+l2

Oct 25 '07 #3

"Dan Bishop" <da*****@yahoo.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
On Oct 24, 8:56 pm, "Junior" <mlon...@verizon.netwrote:
>I want to open a text file for reading and delineate it by comma. I also
want any data
surrounded by quotation marks that has a comma in it, not to count the
commas inside the
quotation marks

Use the csv module.
Thanks for the help! I used the csv module to write this;

import csv
reader = csv.reader(open('testfile.txt', "rb"))
for row in reader:
print "var2= ", row[2]
The reason I didn't use the csv module is because I read this book to learn
Python,

Python Programming for the Absolute Beginner, Second Edition (For the
Absolute Beginner)
by Michael Dawson (Author),

and it did not mention the csv module it just explained how to import
modules. I also read

the tutorial that comes with python but it ends with the History and License
section.

Can you recommend a book that explains the most used modules?

Oct 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Alberto Vera | last post: by
8 posts views Thread by Stephen Poley | last post: by
63 posts views Thread by Tristan Miller | last post: by
9 posts views Thread by Marek Mänd | last post: by
9 posts views Thread by Wayne | last post: by
31 posts views Thread by The Bicycling Guitarist | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.