Write a list in a python csv file, one new row per list

后端 未结 2 663
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 08:11

I have the following source code, where I am trying to write a list in a csv file. I need every new list to be written in a new line of this csv file. The source code is the

2条回答
  •  心在旅途
    2020-12-30 08:36

    If you use Python 3.x then change your code:

    import csv
    
    list1 = [58,100,'dir1/dir2/dir3/file.txt',0.8]
    
    with open("output.csv", "a", newline='') as fp:
        wr = csv.writer(fp, dialect='excel')
        wr.writerow(list1)
    

    Adding newline='' can help you to avoid getting extra new lines, empty rows in between two rows with data.

提交回复
热议问题