问题
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?
回答1:
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)
来源:https://stackoverflow.com/questions/64190592/python-csv-how-to-append-data-at-the-end-of-a-row-whilst-reading-it-line-by-lin