1、查询CSV模块中都有哪些函数和功能?
① 通过dir()函数查询CSV模块都有哪些函数
import csv
for i in dir(csv):
print(i)
② 搜索CSV模块的官方教程
’https://docs.python.org/3.6/library/csv.html‘
③ 搜索中文教材
’https://yiyibooks.cn/xx/python_352/library/csv.html#module-csv‘
2、读取CSV文件
import csv
with open("test.csv",'r',newline = '',encoding='utf-8') as f:
reader = csv.reader(f)
#使用csv的reader()方法,创建一个reader对象
for row in reader:
#遍历reader对象的每一行
print(row)
3、写入CSV文件
import csv
with open('test.csv','a',newline='',encoding='utf-8') as f:
#test.csv代表文件路径;a代表追加写入;encoding指定编码方式
writer = csv.writer(f) #实例化writer对象
writer.writerow([1,2,3,4,5])
#调用writerow方法写入一行数据
writer.writerows([['4', '猫砂', '25', '1022', '886'],['5', '猫罐头',

本文主要探讨了Python中的CSV模块,包括如何查询模块函数、读取和写入CSV文件,以及split()和join()函数的用法。通过官方教程和中文教材,详细介绍了CSV模块在处理CSV数据时的功能。
最低0.47元/天 解锁文章
6765

被折叠的 条评论
为什么被折叠?



