如何编写列表的列表在csv文件在python [英] How to write List of lists in csv file in python

查看:138
本文介绍了如何编写列表的列表在csv文件在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表,我想把它写在csv文件中
示例列表:

  = [['serial','name','subject'],['1','atul','tpa'],['2','carl','CN' ] 

data [0]应该是列名
一切都是行数据<

解决方案

这是微不足道的与 csv 模块

  with open('output.csv','w',newline ='')as csvfile:
writer = csv.writer (csvfile)
writer.writerows(data)

c $ c> data 作为第一行;您可以使用 writer.writerows()方法一次性写入所有行。这一切都是真的。


I have a list of lists and I want to write it in csv file Example list:

data=[['serial', 'name', 'subject'],['1', 'atul','tpa'],['2', 'carl','CN'].......]

data[0] should be column names everything else is row wise data

Please suggest me a way to do this.

解决方案

This is trivial with the csv module:

with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(data)

You already have the header in data as the first row; you can write all rows in one go with the writer.writerows() method. That's all there is to it, really.

这篇关于如何编写列表的列表在csv文件在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆