2

I am reading a CSV file called: candidates.csv line by line (row by row) like follows:

import csv
for line in open('candidates.csv'):
    csv_row = line.strip().split(',')
    check_update(csv_row[7]) #check_update is a function that returns an int

How can I append the data that the check_updates function returns at the end of the line (row) that I am reading? Here is what I have tried:

for line in open('candidates.csv'):
    csv_row = line.strip().split(',')
    data_to_add = check_update(csv_row[7])
    with open('candidates.csv','a') as f:
        writer = csv.writer(f)
        writer.writerow(data_to_add)

Getting this error:

_csv.Error: iterable expected, not NoneType

Also not entirely sure that this would have added in the right place at the end of the row that I was reading.

Bottom line, how to best add data at the end of the row that I am currently reading?

Pro Girl
  • 762
  • 7
  • 21

1 Answers1

2

Do backup your file before trying just in case.

You can write a new temporary file and move that into the place over the old file you read from.

from tempfile import NamedTemporaryFile
import shutil
import csv

filename = 'candidates.csv'
tempfile = NamedTemporaryFile('w', delete=False)

with open(filename, 'r', newline='') as csvFile, tempfile:
    writer = csv.writer(tempfile)

    for line in csvFile:
        csv_row = line.strip().split(',')
        csv_row.append(check_update(csv_row[7])) # this will add the data to the end of the list.
        writer.writerow(csv_row)

shutil.move(tempfile.name, filename)
Faizan
  • 268
  • 2
  • 9
  • Hi Faizan, I am getting this error: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\COBRAC~1\\AppData\\Local\\Temp\\tmpa5gh3d1j' – Pro Girl Oct 04 '20 at 03:13
  • Make sure that `shutil.move` is called outside the `with` block. – Faizan Oct 04 '20 at 03:55