473,379 Members | 1,302 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,379 software developers and data experts.

Need help converting text to csv format

Hey guys. I'm working on a little program to help my wife catalog her/
our coupons. I found a good resource but need help formatting the
text data so that I can import it into a mysql database. Here's the
data format:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

The first value (large number) is the UPC, the next element is the
coupon description, followed by a date in parenthesis. Those are the
only three elements I am concerned with. Can someone help me in
reformatting this:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

into something like this:

"409220000003","Life Fitness Products $1","12-13-08"
"546500181141","Oust Air Sanitizer, any B1G1F up to $3.49","1-17-09"
"518000159258","Pillsbury Crescent Dinner Rolls, any .25","2-14-09"
"518000550406","Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40","2-14-09"

Any help, pseudo code, or whatever push in the right direction would
be most appreciated. I am a novice Python programmer but I do have a
good bit of PHP programming experience.

Thanks for your time....
Nov 21 '08 #1
22 3997
On Nov 21, 10:18*am, Chuck Connors <don...@gmail.comwrote:
Any help, pseudo code, or whatever push in the right direction would
be most appreciated. *I am a novice Python programmer but I do have a
good bit of PHP programming experience.
I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...
Nov 21 '08 #2
I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...
Thanks for the push in the right direction, friend.

Nov 21 '08 #3
409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

into something like this:

"409220000003","Life Fitness Products $1","12-13-08"
"546500181141","Oust Air Sanitizer, any B1G1F up to $3.49","1-17-09"
"518000159258","Pillsbury Crescent Dinner Rolls, any .25","2-14-09"
"518000550406","Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40","2-14-09"

Any help, pseudo code, or whatever push in the right direction would
be most appreciated. I am a novice Python programmer but I do have a
good bit of PHP programming experience.
A regexp should be able to split this fairly neatly:

import re
r = re.compile(r"^(\d+)\s+(.*)\((\d{1,2}-\d{1,2}-\d{2,4})\).*")
out = file('out.csv', 'w')
for i, line in enumerate(file('in.txt')):
m = r.match(line)
if not m:
print "Line %i is malformed" % (i+1)
continue
out.write(','.join(
'"%s"' % item.strip().replace('"', '""')
for item in m.groups()
))
out.write('\n')
out.close()

-tkc

Nov 21 '08 #4
On Nov 21, 2008, at 8:18 AM, Chuck Connors wrote:
The first value (large number) is the UPC, the next element is the
coupon description, followed by a date in parenthesis. Those are the
only three elements I am concerned with. Can someone help me in
reformatting this:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
Well, you could use regex to parse out those three parts... on the
other hand, str.partition might be a little easier if you're not
already used to regex. Try this:
>>line = "409220000003 Life Fitness Products $1 (12-13-08) (CVS)"
num,sep,rest = line.partition(' ')
num
'409220000003'
>>rest
'Life Fitness Products $1 (12-13-08) (CVS)'
>>desc,sep,rest = rest.partition('(')
desc
'Life Fitness Products $1 '
>>rest
'12-13-08) (CVS)'
>>date,sep,rest = rest.partition(')')
date
'12-13-08'

So three lines of code gets you your three fields.
into something like this:

"409220000003","Life Fitness Products $1","12-13-08"
There are lots of ways to do that. Here's one:
>>qfields = ['"' + fld.strip() + '"' for fld in (num,desc,date)]
out = qfields.join(',')
out
'"409220000003","Life Fitness Products $1 ","12-13-08"'

Cheers,
- Joe

P.S. Pay no attention to the grumpy graybeards who want to drive new
users away from Python. There are quite a few friendly and helpful
people here, too. :)

Nov 21 '08 #5
Wow! What a change in direction from the previous post. Thank you
both for the help and the explanations. This will work great!
Nov 21 '08 #6
George Sakkis wrote:
On Nov 21, 10:18 am, Chuck Connors <don...@gmail.comwrote:
>Any help, pseudo code, or whatever push in the right direction would
be most appreciated. I am a novice Python programmer but I do have a
good bit of PHP programming experience.

I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...
I'm wondering why you bothered to write that. Next time, save yourself
the ten seconds and just skip to the next message. The world will be a
better place.

though-we-all-get-crabby-sometimes-ly y'rs - steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 21 '08 #7
>>qfields = ['"' + fld.strip() + '"' for fld in (num,desc,date)]
>>out = qfields.join(',')
Just a quick note here to prevent the confusion of the OP...this
should be

','.join(qfields)

-tkc


Nov 21 '08 #8
Tim Chase wrote:
> >>qfields = ['"' + fld.strip() + '"' for fld in (num,desc,date)]
out = qfields.join(',')

Just a quick note here to prevent the confusion of the OP...this should be

','.join(qfields)
To be honest, it's so easy to use the stdlib csv module
that I'd always recommend that, especially as it covers
all those annoying corner cases with embedded commas and
quotes and stuff. And it's tested to hell and back.
In general, for a list of field tuples:

<code>
import csv

fields = [
("Tim Golden", 40, "inf,o1"),
("Fred Smith", 25, "info2"),
("Joe O'Reilly", 55, 'inf,"blah",o3'), ## lots of messiness
]

ofile = open ("data.csv", "wb")
try:
writer = csv.writer (ofile)
writer.writerows ([[str (i) for i in f] for f in fields])
finally:
ofile.close ()

</code>
Nov 21 '08 #9
On Nov 21, 2008, at 9:22 AM, Tim Golden wrote:
Tim Chase wrote:
>>>qfields = ['"' + fld.strip() + '"' for fld in (num,desc,date)]
out = qfields.join(',')
Just a quick note here to prevent the confusion of the OP...this
should be
','.join(qfields)
Thanks Tim #1, for pointing out my error (my standard procedure with
join is to [1] do it backwards, [2] curse myself, and [3] do it
correctly, but it looks like I only copied step 1 into my email).
To be honest, it's so easy to use the stdlib csv module
that I'd always recommend that, especially as it covers
all those annoying corner cases with embedded commas and
quotes and stuff. And it's tested to hell and back.
And thanks Tim #2, for pointing that out. I had frankly forgotten
about it, but that's good advice.
finally:
ofile.close ()
A follow-up question here... is it really necessary to close things
like files in Python? I've been slumming it in the REALbasic
community for the last decade, where you generally don't worry about
such things, as any object that represents something "open" will
automatically "close" itself when it dies (and since a closed object
in those cases is useless, I'd rather not have it around after it's
closed anyway). Is the same true in Python, or do we need to
explicitly close things?

Thanks,
- Joe

Nov 21 '08 #10
Joe Strout wrote:
A follow-up question here... is it really necessary to close things like
files in Python? I've been slumming it in the REALbasic community for
the last decade, where you generally don't worry about such things, as
any object that represents something "open" will automatically "close"
itself when it dies (and since a closed object in those cases is
useless, I'd rather not have it around after it's closed anyway). Is the
same true in Python, or do we need to explicitly close things?

Implementation dependent. (Because it depends on what kind
of garbage collection or object finalisation happens). Like
most people, I imagine, in ad-hoc code I'll just do things
like:

<code
import csv

writer = csv.writer (open ("data.csv", "wb"))
writer.writerow (['blah', blah'])

</code>

If I exit the interpreter here, I'm pretty much safe.
But if I add os.startfile ("data.csv"), I'll likely
get nothing or a file lock.

I believe that in other implementations -- Jython,
for example -- you cannot rely on the file closing
itself.

In general in posting public code, especially to
newcomers, I make the effort to use a try-finally
or a with statement.

TJG
Nov 21 '08 #11
Tim Golden wrote:
Tim Chase wrote:
>> >>qfields = ['"' + fld.strip() + '"' for fld in (num,desc,date)]
>>out = qfields.join(',')
Just a quick note here to prevent the confusion of the OP...this should be

','.join(qfields)

To be honest, it's so easy to use the stdlib csv module
that I'd always recommend that, especially as it covers
all those annoying corner cases with embedded commas and
quotes and stuff. And it's tested to hell and back.
[cedes point to TimG]

yes, the CSV module has some wonderful stuff in it, and I
regularly use it for *reading* CSV files. But for writing them,
it's often just as fast (for my purposes) to simply code my 1st
post's quickie as it is to scrounge in the docs/docstrings to
remember how to let the CSV do the creation. But TimG is correct
that using csv is the _right_ way to do it...gotta leave SOME
work for the OP :)

too-many-tims-on-c.l.p'ly yers,

-tkc
Nov 21 '08 #12
Tim Chase wrote:
yes, the CSV module has some wonderful stuff in it, and I regularly use
it for *reading* CSV files. But for writing them, it's often just as
fast (for my purposes) to simply code my 1st post's quickie as it is to
scrounge in the docs/docstrings to remember how to let the CSV do the
creation. But TimG is correct that using csv is the _right_ way to do
it...gotta leave SOME work for the OP :)
Well I was going to point out that it's not that hard to remember
the functions, when I realised that I needn't have str ()-ed all
those values since the module does that for you. As it happens I
usually find it easy enough to remember.

But I think it's one of those
modules where you tend to think: why bother? it's only a
",".join (str (i) for i in f) away. Until you hit embedded quotes
or irregular delimiters. And you realise you've spawned a
monster genexp. And *then* you reach for "import csv" :)

TJG
Nov 21 '08 #13
Tim Golden wrote:
Joe Strout wrote:
>A follow-up question here... is it really necessary to close things
like files in Python? I've been slumming it in the REALbasic
community for the last decade, where you generally don't worry about
such things, as any object that represents something "open" will
automatically "close" itself when it dies (and since a closed object
in those cases is useless, I'd rather not have it around after it's
closed anyway). Is the same true in Python, or do we need to
explicitly close things?


Implementation dependent. (Because it depends on what kind
of garbage collection or object finalisation happens). Like
most people, I imagine, in ad-hoc code I'll just do things like:

<code
import csv

writer = csv.writer (open ("data.csv", "wb"))
writer.writerow (['blah', blah'])

</code>

If I exit the interpreter here, I'm pretty much safe.
But if I add os.startfile ("data.csv"), I'll likely
get nothing or a file lock.

I believe that in other implementations -- Jython,
for example -- you cannot rely on the file closing
itself.
The file will be closed automatically when the file object is
garbage-collected.

CPython uses reference-counting, so the file object is garbage-collected
as soon as there are no references to it.

Jython (and IronPython?) are garbage-collected in the background, so the
file object is garbage-collected at some point (and you don't know when
that will be!) when there are no longer any references to it.
In general in posting public code, especially to
newcomers, I make the effort to use a try-finally
or a with statement.
Nov 21 '08 #14
On 2008-11-21 16:18, Chuck Connors wrote:
Hey guys. I'm working on a little program to help my wife catalog her/
our coupons. I found a good resource but need help formatting the
text data so that I can import it into a mysql database. Here's the
data format:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

The first value (large number) is the UPC, the next element is the
coupon description, followed by a date in parenthesis. Those are the
only three elements I am concerned with. Can someone help me in
reformatting this:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

into something like this:

"409220000003","Life Fitness Products $1","12-13-08"
"546500181141","Oust Air Sanitizer, any B1G1F up to $3.49","1-17-09"
"518000159258","Pillsbury Crescent Dinner Rolls, any .25","2-14-09"
"518000550406","Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40","2-14-09"

Any help, pseudo code, or whatever push in the right direction would
be most appreciated. I am a novice Python programmer but I do have a
good bit of PHP programming experience.
Have a look at the csv module:

http://www.python.org/doc/2.5.2/lib/module-csv.html

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Nov 21 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
__________________________________________________ ______________________
2008-11-12: Released mxODBC.Connect 0.9.3 http://python.egenix.com/

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Nov 21 '08 #15
On Nov 21, 2008, at 10:26 AM, MRAB wrote:
The file will be closed automatically when the file object is
garbage-collected.

CPython uses reference-counting, so the file object is garbage-
collected as soon as there are no references to it.

Jython (and IronPython?) are garbage-collected in the background, so
the file object is garbage-collected at some point (and you don't
know when that will be!) when there are no longer any references to
it.
Thanks all for this clarification. That is an important distinction.
(REALbasic uses reference-counting, so you can guarantee that the file
will be closed as soon as it loses its last reference, but I see that
we shouldn't count on that in the Python world.)

Best,
- Joe

Nov 21 '08 #16
On Nov 21, 11:05*am, Steve Holden <st...@holdenweb.comwrote:
George Sakkis wrote:
On Nov 21, 10:18 am, Chuck Connors <don...@gmail.comwrote:
Any help, pseudo code, or whatever push in the right direction would
be most appreciated. *I am a novice Python programmer but I do have a
good bit of PHP programming experience.
I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...

I'm wondering why you bothered to write that. Next time, save yourself
the ten seconds and just skip to the next message. The world will be a
better place.
RTFM and doing an elementary search is an even better way to making
the world a better place.

George
Nov 21 '08 #17
George Sakkis <ge***********@gmail.comwrites:
On Nov 21, 11:05Â*am, Steve Holden <st...@holdenweb.comwrote:
>George Sakkis wrote:
On Nov 21, 10:18 am, Chuck Connors <don...@gmail.comwrote:
>Any help, pseudo code, or whatever push in the right direction would
be most appreciated. Â*I am a novice Python programmer but I do have a
good bit of PHP programming experience.
I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...

I'm wondering why you bothered to write that. Next time, save yourself
the ten seconds and just skip to the next message. The world will be a
better place.

RTFM and doing an elementary search is an even better way to making
the world a better place.

George
So you will be replying in this tone to each and every question which
has an answer discoverable by Google and in depth knowledge of the "FM"
then I assume?
Nov 21 '08 #18
r0g
Chuck Connors wrote:
Hey guys. I'm working on a little program to help my wife catalog her/
our coupons. I found a good resource but need help formatting the
text data so that I can import it into a mysql database. Here's the
data format:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

The first value (large number) is the UPC, the next element is the
coupon description, followed by a date in parenthesis. Those are the
only three elements I am concerned with. Can someone help me in
reformatting this:

409220000003 Life Fitness Products $1 (12-13-08) (CVS)
546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
each
518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40 (2-14-09)

into something like this:

"409220000003","Life Fitness Products $1","12-13-08"
"546500181141","Oust Air Sanitizer, any B1G1F up to $3.49","1-17-09"
"518000159258","Pillsbury Crescent Dinner Rolls, any .25","2-14-09"
"518000550406","Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
Cinnamon Rolls, etc. .40","2-14-09"

Any help, pseudo code, or whatever push in the right direction would
be most appreciated. I am a novice Python programmer but I do have a
good bit of PHP programming experience.

Thanks for your time....

Hi Chuck,

Don't be put off, not everyone here is rude and what you are asking
isn't so trivial as to justify that flaming RTFM horsecrap. You are
moving from php to python - this is cause for celebration! Welcome :-)

Anyway I can see two ways of attacking this, you could either write a
finite state machine or use regular expressions. My preference would be
regular expressions.

The following should do what you want although I haven't tested it much
and you might want to lookout for irregularities in your source text
like linebreaks etc...

import re
regex = re.compile("([0-9]{3,13}) ([^(]*) \(([^)]*)")

for each in regex.findall(source_data):
print '"'+each[0]+'","'+each[1]+'","'+each[2]+'"'
I don't know if you already know regular expressions from some other
language so I won't bother explaining this one other than to say you may
note Pythons regex syntax is generally more liberal than many other
languages implementations. If this is all new to you and you do need
this explaining just let me know and I will happily run you through it.
Also, you say your eventual target is a SQL database. If the database is
local (or reachable via a network) then you are probably going to be
better off using python to connect to it directly using the MySQLdb
module...

import MySQLdb

db = MySQLdb.connect(host="localhost",
user="uname",
passwd="pwd",
db="dbname")

cursor = db.cursor()
cursor.execute("INSERT YOU SQL QUERY HERE")
db.commit ()
Hope this helps,

Roger.
Nov 21 '08 #19
On Nov 21, 2:01*pm, Richard Riley <rileyrg...@gmail.comwrote:
George Sakkis <george.sak...@gmail.comwrites:
On Nov 21, 11:05*am, Steve Holden <st...@holdenweb.comwrote:
George Sakkis wrote:
On Nov 21, 10:18 am, Chuck Connors <don...@gmail.comwrote:
Any help, pseudo code, or whatever push in the right direction would
be most appreciated. *I am a novice Python programmer but I do have a
good bit of PHP programming experience.
I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...
I'm wondering why you bothered to write that. Next time, save yourself
the ten seconds and just skip to the next message. The world will be a
better place.
RTFM and doing an elementary search is an even better way to making
the world a better place.
George

So you will be replying in this tone to each and every question which
has an answer discoverable by Google and in depth knowledge of the "FM"
then I assume?
No, only to those which is blatantly obvious that they didn't put any
effort whatsoever to find an answer on their own; "python csv" has
1,420,000 hits in Google, the first one linking to the csv module
docs. Feel free to check out my 4-year posting history in c.l.py to
get a more accurate idea of the usefulness and tone of my posts.

George
Nov 21 '08 #20
On Nov 22, 7:38*am, George Sakkis <george.sak...@gmail.comwrote:
On Nov 21, 2:01*pm, Richard Riley <rileyrg...@gmail.comwrote:
George Sakkis <george.sak...@gmail.comwrites:
On Nov 21, 11:05*am, Steve Holden <st...@holdenweb.comwrote:
>George Sakkis wrote:
On Nov 21, 10:18 am, Chuck Connors <don...@gmail.comwrote:
>Any help, pseudo code, or whatever push in the right direction would
>be most appreciated. *I am a novice Python programmer but I do have a
>good bit of PHP programming experience.
I'm wondering if PHP experience precludes the ability to use a search
engine before asking for help...
>I'm wondering why you bothered to write that. Next time, save yourself
>the ten seconds and just skip to the next message. The world will bea
>better place.
RTFM and doing an elementary search is an even better way to making
the world a better place.
George
So you will be replying in this tone to each and every question which
has an answer discoverable by Google and in depth knowledge of the "FM"
then I assume?

No, only to those which is blatantly obvious that they didn't put any
effort whatsoever to find an answer on their own; "python csv" has
1,420,000 hits in Google, the first one linking to the csv module
docs.
If I had to write such a script in Python, I'd be using the re and csv
modules in similar fashion to responses already posted. If I had to
write it in PHP, it'd be my first PHP script, so let's go Googling:

google("php regex") first hit:
http://www.regular-expressions.info/php.html
[yuk, 3 different toolkits; 3rd is PCRE, let's try that]

google("php csv") 3rd hit points to:
http://www.php.net/manual/en/function.fputcsv.php

Doesn't seem too difficult to me.

I'd be interested to hear from the OP what is his level of experience
in PHP, how he would have approached the problem in PHP, and why he
didn't look for similar ways in Python.
Nov 21 '08 #21
r0g
>>>>>On Nov 21, 10:18 am, Chuck Connors <don...@gmail.comwrote:
>>>>>>Any help, pseudo code, or whatever push in the right direction would
>>be most appreciated. I am a novice Python programmer but I do have a
>>good bit of PHP programming experience.
<snip>
John Machin wrote:
If I had to write such a script in Python, I'd be using the re and csv
modules in similar fashion to responses already posted. If I had to
write it in PHP, it'd be my first PHP script, so let's go Googling:

google("php regex") first hit:
http://www.regular-expressions.info/php.html
[yuk, 3 different toolkits; 3rd is PCRE, let's try that]

google("php csv") 3rd hit points to:
http://www.php.net/manual/en/function.fputcsv.php

Doesn't seem too difficult to me.
I'm sure it doesn't, it doesn't to me either, but only because we
already know what he needs! Do you think if the OP knew all about
regular expressions and understood that python has a handy csv module
that he'd be asking this question here?

The OP is used to PHP, a fairly different kettle of fish. Disgusting
though PHP is it is many peoples first programming language, often in
the unpleasant form of mod_php on a web server - clearly we are not
dealing with a linux kernel hacker here.

I don't think it's at all unreasonable to come here and ask for "help,
pseudo code, or whatever push in the right direction", and I certainly
don't see a need to start questioning his credentails.

He needs to know about the re and csv modules. A minimal yet polite
response might have been: Go and google for the re and csv modules. You
could have typed that, saved yourself a few hundred keystrokes and
helped someone in the process.

If you can't muster even that level of enthusiasm or courtesy towards
the noobs then you always have the option of not responding.

:-/

Roger.
Nov 22 '08 #22
On Nov 22, 2:28*am, Joe Strout <j...@strout.netwrote:
A follow-up question here... is it really necessary to close things *
like files in Python?
Not if you use a context manager (2.6+, available in 2.5 from
__future__):

with open('data.csv', 'wb') as csvfile:
writer = csv.writer(csvfile)
...

The file is guaranteed to be closed at the end of the with-statement
block.
Nov 22 '08 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David L | last post by:
Hi, I am wondering that whether the fact that as more tools/environments/products support XML, then the need for knowing XML itself gets less important. I am comparing xml to assembler. IT...
1
by: Jonny Au | last post by:
Hi everyone, I have a problem about email message format converting. I want to write a program in VB.NET to convert the EML format email message to MSG format, does anyone knows how to do it?...
3
by: Francesc | last post by:
Hi, I'm new at this newsgroup and I want do ask some questions and opinions about this subject. I'm developing an application focused in a very specific task: clean and labelling text documents...
7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
6
by: B.N.Prabhu | last post by:
Hi, I am having a DataTable which one column is in string format now i want to change that one to DataTime. I don't know whether its in datetime format or not i have to check if it is in...
2
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
1
by: ganesh22 | last post by:
Hi, Iam getting the below error while my application is running on IIS. in my application iam converting a text into word format, so i added some .dll from COM for converting word format ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.