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
inefficient.
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
w.writerow(row)
f.close()
updateLine(idVar = "Id",
idValue = "hawxgXvbfu",
myCsv = "c:/temp/someCsv.csv",
newLine = [ch for ch in string.letters[0:9]])
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor