简体   繁体   中英

Append only 1 row to CSV file each time

I use:

f = open("my_file.csv", "a")
writer = csv.writer(f)
writer.writerow(['one', 'two'])

But instead of appending a new row after the last row, it appends 2 new rows: an empty one and then the required one "one| two". What is the reason for that?

I find it easier to use with open() when writing to csv files. The documentation can be found here . I can reproduce the problem you had on my machine, but the following code does not produce an empty line after each new line is appended.

import csv

with open('my_file.csv', 'a', newline='') as csvfile:      
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(['one', 'two'])

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