python3操作csv文件
从python2转到python3,操作csv文件时,绝对是个坑,下面在例子中讲解。
假设我有一个csv文件test.csv,内容为:
1、python3读取该文件的代码为:
import csv with open('D:/Users/lizj9/test.csv', 'r') as f: read = csv.reader(f) for now in read: print(now)
执行结果为:
2、python3向该test.csv文件写入代码:
同样在python2中写入文件时,用’wb’方式写入。
在python3中默认读取和写入csv文件是以文本方式,默认 ‘utf-8’
转自:https://www.dianjilingqu.com/