简体   繁体   English

CSV档案中的Python清单

[英]Python list in csv file

I have a question about how to save a list as a CSV file in my way: 我有一个关于如何以我的方式将列表另存为CSV文件的问题:

If I have a big list with many sub-lists: records = [['a','b','c','d'], ['1','2','3','4'], ......] 如果我有一个包含许多子列表的大列表:记录= [['a','b','c','d'],['1','2','3','4'] ,......]

and i use this code to generate a csv file: 我使用此代码生成一个csv文件:

import csv
create = csv.writer(open(filename, "wb"))
create.writerow(records)

it displays all sub-list in one row and each one is in one cell: ['a','b','c','d'] ['1','2','3','4']..... 它在一行中显示所有子列表,每个子列表都在一个单元格中:['a','b','c','d'] ['1','2','3','4' ] .....

However i don't want the square brackets and quotation marks, but want every item in one cell, and only one sub-list per row: a,b,c,d 1,2,3,4 但是我不想要方括号和引号,而是想要每个单元格中的每个项目,并且每行仅一个子列表:a,b,c,d 1,2,3,4

I'm new to python and really know few about the codes. 我是python的新手,对代码的了解很少。 hope u can help me :) 希望你能帮助我:)

with open(filename, "wb") as file:
    csv.writer(file).writerows(records)
import csv
create = csv.writer(open(filename, "wb"))
for element in record:
    create.writerow(element)

Should give you what you want. 应该给你你想要的。

You need to iterate over the records structure. 您需要遍历records结构。 Something like this: 像这样:

for x in records:
    create.writerow(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM