473,795 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,"Jun e,July and August",Indepen dance Day
and I write the following program

fname = open('testfile. txt', 'r')
ct=1
while ct != 3:
rd=(fname.readl ine())
filedata=rd.spl it(',')
print "var2=",filedat a[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 2229
On Oct 24, 8:56 pm, "Junior" <mlon...@verizo n.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...@verizo n.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,"Jun e,July and August",Indepen dance Day

and I write the following program

fname = open('testfile. txt', 'r')
ct=1
while ct != 3:
rd=(fname.readl ine())
filedata=rd.spl it(',')
print "var2=",filedat a[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').re ad().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.goo glegroups.com.. .
On Oct 24, 8:56 pm, "Junior" <mlon...@verizo n.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
4576
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
3040
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 on my desk, I've produced a couple of sed scripts to automatically convert the 'typewriter' equivalents: http://www.xs4all.nl/~sbpoley/webmatters/proper_quotes.html I've tested them fairly thoroughly, but I'd be grateful if a few other
63
5913
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 attribute of the HTML document. Will any browsers render German-style quotes or French-style guillemots for lang="de" and lang="fr", respectively? Regards, Tristan
9
3756
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 would be no comma right after HERE</q>
3
1881
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 simple .txt file. I then use that file to keep the project information current for the multiple AutoCAD drawings that project requires. My problem.....several of the items I save are company names (customers). Many times company names have commas...
1
9757
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 control. It outputs all text fields surrounded by quotes, and all numeric fields w/o quotes. All fields are separated with commas. This has been working for 2 years, until today, when one of the data fields
9
4515
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 city state. Is there a built in function that I'm missing that will do the split and recognize that the comma in city state is part of the item? --
31
4894
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 using &ldquo; and &rdquo; (in pairs as needed) instead. Are there any *disadvantages* to doing so, besides the time it will take to make the change? Thanks.
2
4266
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
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.