How do I read and write to the same CSV file

C

Chris

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?
 
M

Marc 'BlackJack' Rintsch

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?

You can't. Modifying text files with variable length lines is
"impossible" or at least not worth the trouble because if the replacement
line does not have the very same length, all lines after that changed line
have to be moved, i.e. rewritten anyway.

Write the rows to another file and modify the desired entry on the fly and
rename the temporary file to the source file's name afterward.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steven D'Aprano

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?

With difficulty unless the bytes you are writing to are exactly the same
length as the bytes that were there before.

In my opinion, unless you're writing a file with fixed-length fields, and
maybe not even then, give up the dream of writing directly in place.
Unless your file is HUGE, the chances are that trying to modify it in
place is not only harder work but also slower.

But if you insist... you might be able to use cf_stream.seek() to move to
the current position in the file, then write the bytes you want. How do
you find the current position? Black magic or voodoo might help.
Otherwise you might count every character of every line up to the field
you want. Don't forget delimiters, quote marks and newlines. Good luck.
 
S

Steven D'Aprano

But if you insist... you might be able to use cf_stream.seek() to move
to the current position in the file, then write the bytes you want. How
do you find the current position? Black magic or voodoo might help.

<slaps head>

Well, that'll learn me to post at ungodly hours of the morning. Getting
the _current_ position is easy, what I meant to write was CORRECT
position. Twice.

How embarrassment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top