赞
踩
CSV(逗号分隔值)格式是电子表格和数据库最常见的导入和导出格式,
它允许程序员说“以 Excel 首选的格式写入此数据”或“从 Excel 生成的此文件中读取数据”,而无需了解 Excel 使用的 CSV 格式的确切细节。其中再wps以及微软的Excel这两个软件中,数据内容的格式是并不兼容的
- wps中要用 utf-8 写入
- Excel要用 utf-8-sig 写入
当我们想要对CSV文件进行操作时,我们可以导入python自带的csv库进行操作
import csv
import csv
header = ['name', 'url', 'city']
data = [['才短思涩-求解', 'https://blog.csdn.net/caribbean666?type=blog', '保定']]
with open('ggg.csv', 'w', encoding='utf-8', newline='') as f:
#创建文件
write = csv.writer(f)
# 单行写入
write.writerow(header)
# 多行写入
write.writerows(data)
import csv
header = ['name', 'url', 'city']
data = [{'name': '才短思涩-求解', 'url': 'https://blog.csdn.net/caribbean666?type=blog', 'city': '保定'}]
with open('dict.csv', 'w', encoding='utf-8', newline='') as f:
#创建文件
write = csv.DictWriter(f, header)
#声明表头
write.writeheader()
# 单行写入
# write.writerow(data)
# 多行写入
write.writerows(data)
如果需要写入很多数据时只需要写入一次表头,那么可以再write.writeheader()加一个if判断
import csv k=1 header = ['name','url','city'] data=[{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'}{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'}{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},] with open('dict.csv','w',encoding='utf-8',newline='') as f: write=csv.DictWriter(f,header) # 声明表头 if k==1: write.writeheader() k+=1 # 单行写入 # write.writerow(data) # 多行写入 write.writerows(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
import csv
with open('dict.csv', 'r', encoding='utf-8') as f:
#创建读取对象
read = csv.reader(f)
# print(read)# 生成器
for i in read:
print(i)
import csv
with open('dict.csv', 'r', encoding='utf-8') as f:
read = csv.DictReader(f) #生成器
# print(read)
for i in read:
print(i)
注意: 打开文件时应指定格式为w, 文本写入. 打开文件时,指定不自动添加新行newline=‘’,否则每写入一行就或多一个空行。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。