Write to CSV on first empty line

This question already has an answer here:

  • How do you append to a file? 8 answers

  • Try this instead (note the 'a' for append):

    with open('C:/Users/Family3/Downloads/weather.csv', 'a', newline='') as csvfile:
        writer = csv.writer(csvfile, delimiter=',',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(listsof)
    

    The 'a' opens the file for appending and does not erase the already existing file with the same name.


    It is because, whenever you open your csv file, you never take into account that you might already have data in your file. When you load your csvfile into your program, you should append your new data to the file instead of just writing to it.

    链接地址: http://www.djcxy.com/p/42396.html

    上一篇: 在python 3中写入文本文件的末尾

    下一篇: 在第一个空行写入CSV