1

I have a csv file, from which I will read row by row and for certain field the data need to be processed and insert the results into another field in the same row before moving on to the next field.

I tried various methods like:

w = open('test.csv', 'w+')
csv_r = csv_reader(w)
csv_w = csv.writer(w)

for row in csv_r:
    row[10] = results
    csv_w.writerows(row)

But I am getting blank. Is there any other way doing this?

Basically i need to read a specific element in a row and then process the data and the result will be appended into another element in the same row.

8
  • how many rows does csv_r have initially? Also, how can you be sure that results in not empty(or null) ? Commented Jul 6, 2015 at 9:43
  • @anand-s-kumar should be around 20+ rows. And the results will not be null, as I will check if null insert "empty" string as the results Commented Jul 6, 2015 at 9:46
  • there is no way from what you have provided that you should get an empty csv file once csv_r is not empty Commented Jul 6, 2015 at 9:47
  • @PadraicCunningham But I am writing using csv_w Commented Jul 6, 2015 at 9:49
  • 2
    what is w supposed to be in csv_r = csv_reader(w)? Commented Jul 6, 2015 at 9:50

2 Answers 2

2

w+ empties/truncates your file so you have nothing to iterate over. It is r+ for reading and writing.

To update the file either store all the updated rows, reopen the file and write or a much better approach is use a tempfile.NamedTemporaryFile to write to then replace the original with shutil.move.

from tempfile import NamedTemporaryFile
from shutil import move
import csv


with open("test.csv") as f, NamedTemporaryFile("w",dir=".", delete=False) as temp:   
    # write all updated rows to out tempfile
    csv_w = csv.writer(out)
    csv_r = csv.reader(f)
    for row in csv_r:
       row[10] = results
       csv_w.writerows(row)
# replace original file with updated
move(temp.name,"test.csv")
Sign up to request clarification or add additional context in comments.

Comments

1

Seems like you are openning both reader and writer on the same file -

csv_r = csv_reader(w)
csv_w = csv.writer(w)

Is that expected? Shouldn't you be openning them on different files?

If you intentionally did it, try using different files, when you open a file with w+ mode, if the file exists it gets overwritten.

w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Try openning the file with r+ mode.

3 Comments

Meaning I make a copy and open them concurrently to make the changes?
no, open the file you want to read in reader, and then open a completely new file in the writer, and then make your changes to lines from reader and writer them into the new csv.
Then if you want to move the new file to overwrite old file use - import shutil; shutil.move('<oldname>','<newname>')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.