简体   繁体   中英

Python CSV Writer only writing last item processed

I am looping through a CSV and creating a new one in 'python3' like this...

for row in csvreader:

    with open('employee_file.csv', mode='w', encoding='utf-8') as employee_file:

        employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        employee_writer.writerow([row[0],employeeName])

But once it finished processing I am left with only the last result in employee_file.csv. Where am I going wrong?

Take the file opening line before the for loop. The way you have it now you keep writing it over in the for loop.

with open('employee_file.csv', mode='w', encoding='utf-8') as employee_file:
   for row in csvreader:
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM