473,287 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 2202
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
8
by: Stephen Poley | last post by:
One disadvantage of using a simple text editor to produce HTML is that it is relatively time-consuming to put in the proper typographical quotation marks and dashes. A round tuit having arrived...
63
by: Tristan Miller | last post by:
Greetings. Do any popular browsers correctly support <q>, at least for Western languages? I've noticed that Mozilla uses the standard English double-quote character, ", regardless of the lang...
9
by: Marek Mänd | last post by:
<style type="text/css"> q:after{content:',"'} </style> <q>This will be the shame of CSS</q> claimed Marek Mänd and added that <q>consumers expect to create generated content via CSS where there...
3
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a...
1
by: DCM Fan | last post by:
Access 2K, SP3 on Windows 2K, SP4 All, I have an import spec set up with quoted Identifiers and comma-separated values. The text file is produced by a 3rd-party program of which I have no...
9
by: Wayne | last post by:
I have the following string: "smith", "Joe", "West Palm Beach, Fl." I need to split this string based on the commas, but as you see the city state contains a comma. String.split will spilt the...
31
by: The Bicycling Guitarist | last post by:
Hi. For many years I have been using &quot; for double quotation marks in the HTML code, but the opening and closing quotation marks render the same in my browser. I'm considering going through and...
2
by: weird0 | last post by:
I have to create a string that contains inverted commas inside it. How can i do that in c#? As follows:- "AT+CMGF=1" string temp=""AT+CMGF=1""; // not valid
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.