python列表写⼊csv⽂件_⽤python把list⾥的数据写⼊csv 展开全部
最常⽤的⼀种⽅法,利⽤pandas包
import pandas as pd#任意的多组列62616964757a686964616fe59b9ee7ad9431333365643662表a = [1,2,3]python怎么读csv数据
b = [4,5,6]
#字典中的key值即为csv中列名dataframe = pd.DataFrame({'a_name':a,'b_name':b})#将DataFrame存储为csv,index表⽰是否显⽰⾏名,_csv("test.csv",index=False,sep=',')1234567891011
a_name  b_name0      1      41      2      52      3      6
同样pandas也提供简单的读csv⽅法,
import pandas as pddata = pd.read_csv('test.csv')12
会得到⼀个DataFrame类型的data。
另⼀种⽅法⽤csv包,⼀⾏⼀⾏写⼊
import csv
#python2可以⽤file替代open
with open("test.csv","w") as csvfile:
writer = csv.writer(csvfile)
#先写⼊columns_name
writer.writerow(["index","a_name","b_name"])
#写⼊多⾏⽤writerows
writer.writerows([[0,1,3],[1,2,3],[2,3,4]])12345678910
index  a_name  b_name0      1      31      2      32      3      41234
读取csv⽂件⽤reader
import csvwith open("test.csv","r") as csvfile:
reader = ader(csvfile)    #这⾥不需要readlines
for line in reader:
print line

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。