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

J

Junior

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
 
D

Dan Bishop

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

equand

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
 
J

Junior

Dan Bishop said:
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?
 
D

Dennis Lee Bieber

Can you recommend a book that explains the most used modules?
Tutorials and beginners books are meant for just that -- introducing
one to the concepts of the language; syntax for looping, branching, data
structures...

The book you should read after those is.... The LIBRARY REFERENCE
Manual (I don't think I've glanced at the Language Reference manual
since version 1.5 <G>). You don't have to memorize the library
reference, but you do need to become familiar with the type of packages
in it, and in how to browse it for information related to whatever
problem is in search a solution.

"Python in a Nutshell" and "Python Essential Reference" essentially
cover parts of the library reference (a selection based upon what the
authors find more important). And these books can rapidly become a bit
"behind" -- a small package may be added to Python version at a point
where the book is already on the way to printer...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top