[Tutor] updating a Csv file

Alan Gauld alan.gauld at btinternet.com
Wed Oct 20 19:11:20 CEST 2010

"Albert-Jan Roskam" <fomcl at yahoo.com> wrote

> I tried updating the old values with new values of exactly the same 
> length (10 characters),
> but that didn't work either

> newLine = [ch for ch in string.letters[0:9]])
> Error: line contains NULL byte

> Id,NaamInstelling,Naam3,Straat,HuisNr,Postcode,Plaats,dummy

There are 8 field headingss here but newline is a list with 9 

> MQrrSzDboW,XWvxiqlrEp,ERQewcVYva,ppBXnpeCOs,HTmVvHRVhH,KHvjNHIYeM,bcEMrYPmuB,w

And this has 8 fields, but they are not all single characters,
nor are they all 10 characters so I'm not sure how you think
you are writing out the same number of characters?

> ...Should I instead just open one file, write to
> another, throw away the original and rename the new file? That seems
> inefficient.

This is by far the most common design pattern. It is safer and allows
you to roll back to the old version in the event of an error (if you 
the original as a .bak or similar).


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list