python+csv操作(读写)import csv
"""
与excel⽂件不同,csv⽂件中:
1.数据都没有数据类型,值都是‘字符串’
2.没有颜⾊和样式,不能指定单元格测的宽⾼,不能合并单元格
3.没有对个⼯作表
4.不能嵌⼊图像图表
"""
readcsv.csv如下:
# data = []
# csvFile = open('readcsv.csv','r')
# reader = ader(csvFile)
# for item in reader:
# # print(item)
# data.append(item)
# print(data)
#
# #关闭csv⽂件
# csvFile.close()
#
#读取本地csv⽂件
# with open('readcsv.csv','r') as csvfile:
# reader1 = ader(csvfile)
# for line in reader1:
# print(line)
csvFile2.csv如下:
#从列表中写⼊csv⽂件 -->从data中读取列表(⼀)
# csvFile2 = open('csvFile2.csv','w',newline='',encoding='utf-8')
# writer = csv.writer(csvFile2)
# m = len(data)
# for i in range(m):
# writer.writerow(data[i])
# csvFile2.close()
#从列表中写⼊csv⽂件 -->从data中读取列表(⼆)
data1 = [['a1',123],['a2',234],['a3',345],['a4',456]]
csvFile2 = open('csvFile2.csv','w',newline='',encoding='utf-8')
writer = csv.writer(csvFile2)
m = len(data1)
for i in range(m):
writer.writerow(data1[i])
csvFile2.close()
#从字典中写⼊
data2 = {'b1':123,'b2':234,'b3':345,'b4':456}
csvFile2 = open('csvFile2.csv','w',newline='',encoding='utf-8')python怎么读csv数据
writer = csv.writer(csvFile2)
for key in data2:
writer.writerow([key,data2[key]])
csvFile2.close()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论