开发者

List to CSV in Python

开发者 https://www.devze.com 2022-12-27 21:43 出处:网络
I am creating a CSV from a list of values. CSV File gets created but the csv is formed as a single column. Actually it should be multiple rows with multiple columns, instead it forms as a multiple row

I am creating a CSV from a list of values. CSV File gets created but the csv is formed as a single column. Actually it should be multiple rows with multiple columns, instead it forms as a multiple rows with开发者_JAVA百科 a single column. I am using the following code

from random import choice
import csv
fileObject = csv.writer(open('Insurance.csv','w'),dialect='excel',delimiter=' ')
for i in range(0,175):
    current_list = list(choice(master_list))
    fileObject.writerows(current_list)
    current_list = []

Thanks


If your intent is to write 175 rows, you need to use:

fileObject.writerow(current_list)

instead of writerows. writerows is used when you have a list of rows (a list of lists), and in this case you have a single row.


Are you sure about this delimiter? If my memory serves, the delimiter in a CSV file should be a comma (hence the name, Comma-Separated Values), and not a space.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号