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

前端 未结 5 1610
独厮守ぢ
独厮守ぢ 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:04

    Well, if you are writing to a CSV file, then why do you use space as a delimiter? CSV files use commas or semicolons (in Excel) as cell delimiters, so if you use delimiter=' ', you are not really producing a CSV file. You should simply construct csv.writer with the default delimiter and dialect. If you want to read the CSV file later into Excel, you could specify the Excel dialect explicitly just to make your intention clear (although this dialect is the default anyway):

    example = csv.writer(open("test.csv", "wb"), dialect="excel")
    

提交回复
热议问题