.csv是一种文件格式【txt,doc】,是一种特殊的文本格式【一组字符串序列,字符之间使用逗号或者使用制表符隔开】
作用:纯文本文件,存储数据,可以在不同的程序之间进行数据的交互
打开方式:记事本,excel
import csv #1.读取csv文件 def readCsv1(path): infoList = [] with open(path,"r") as f: read = csv.reader(f) #获取迭代类型【将打开的文件对象转换为一个可迭代对象】 for item in read: infoList.append(item) return infoList path = r"person.csv" print(readCsv1(path)) #2.向csv文件写入 def writeCsv1(path): infoList = [['username', 'password', 'age', 'address'], ['zhangsan', 'abc123', '10', 'China'], ['lisi', 'aaa123', '18', 'xian']] with open(path,"w",newline = "") as f: write1 = csv.writer(f) #将文件对象转换为可迭代对象 for item in infoList: write1.writerow(item) path = "person.csv" writeCsv1(path)
Pthon之csv(comma separated values,逗号分隔值)文件的读写
最新推荐文章于 2022-12-03 18:33:00 发布