How do I write data to csv file in columns and rows from a list in python?

前端 未结 5 1601
独厮守ぢ
独厮守ぢ 2020-12-15 07:40

everyone.I have a list of lists and I want to write them in a csv file with columns and rows.I have tried the writerows but it isn\'t what I want.An example of my list is th

5条回答
  •  孤城傲影
    2020-12-15 08:02

    The provided examples, using csv modules, are great! Besides, you can always simply write to a text file using formatted strings, like the following tentative example:

    l = [[1, 2], [2, 3], [4, 5]]
    
    out = open('out.csv', 'w')
    for row in l:
        for column in row:
            out.write('%d;' % column)
        out.write('\n')
    out.close()
    

    I used ; as separator, because it works best with Excell (one of your requirements).

    Hope it helps!

提交回复
热议问题