How do I read and write to the same CSV file

Chris chris.olekas at gmail.com
Mon Sep 10 10:27:46 EDT 2007


I want to update one field in a row for a csv file. So far my code
looks something like this

cf_stream = open("\\config.csv","r+")
csv_file = csv.DictReader(cf_stream, ['Algorithm','LastModified'])

and then I know I can do something like this

for row in csv_file
      name = row["Algorithm"]
      date = row["LastModified"]

now what I want is something like this

if date == 0:
      date = os.getmtime()
      # now this is where I want to write into the row of the same csv
file but only updating that one field

How can I do this?




More information about the Python-list mailing list