[Tutor] updating a Csv file
fomcl at yahoo.com
Wed Oct 20 15:29:13 CEST 2010
Hi Steven and Dave,
Thanks for replying to me. Steven, I pasted the traceback below, along with some version info. I'm using Windows XP. I used the very same code as the one I posted before, including the string.letters bit. Btw, thanks for the little 'list' tip.
Dave, what you say makes sense. I tried updating the old values with new values of exactly the same length (10 characters), but that didn't work either (I simply used [ch * 10 for ch in string.letters[0:9]] --list trick not possible here ;-)). I pasted the contents of my test csv file below the traceback.
Anyway, thanks again for your time!
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
Traceback (most recent call last):
File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 18, in <module>
newLine = [ch for ch in string.letters[0:9]])
File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 10, in updateLine
for row in r:
Error: line contains NULL byte
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
--- On Wed, 10/20/10, Dave Angel <davea at ieee.org> wrote:
From: Dave Angel <davea at ieee.org>
Subject: Re: [Tutor] updating a Csv file
To: "Albert-Jan Roskam" <fomcl at yahoo.com>
Cc: "Python Mailing List" <tutor at python.org>
Date: Wednesday, October 20, 2010, 12:23 PM
On 2:59 PM, Albert-Jan Roskam wrote:
> Hi all,
> How can I update a csv file? I've written the code below, but it does
> not work ("Error: line contains NULL byte"). I've never tried opening a
> file in read and write mode (r+) at the same time before, so I suspect
> that this is the culprit. Should I instead just open one file, write to
> another, throw away the original and rename the new file? That seems
> import csv, string
> def updateLine(idVar, idValue, myCsv, newLine):
> f = open(myCsv, "r+")
> r = csv.reader(f)
> w = csv.writer(f)
> header = r.next()
> idPos = header.index(idVar)
> for row in r:
> if row[idPos] == idValue:
> row = newLine
> updateLine(idVar = "Id",
> idValue = "hawxgXvbfu",
> myCsv = "c:/temp/someCsv.csv",
> newLine = [ch for ch in string.letters[0:9]])
In general, you don't want to update anything in place, unless the new
items are guaranteed to be the same size as the old. So when you're
looping through a list, if you replace one item with another, no
problem, but if you insert or delete, then you should be doing it on a copy.
In the file, the byte is your unit of measure. So if what you're
writing might be a different size (smaller or larger), then don't do it
in place. You may not be able to anyway, if the csv module can't handle
it. For example, the file object 'f' has a position, which is set by
either read or write. csv may assume that they can count on that
position not changing from outside influences, in which case even
same-size updates could fail.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor