with open('some.csv','w',encoding="UTF8",newline='') as f:
writer = csv.writer(f)
writer.writerows([[1,2,3],[1,2,3,4]])
写csv文件,newline属性可以使插入数据的过程不会出现不必要的空行。
csvfile = open('some.csv',"r")
read = csv.reader(csvfile)
for row in read:
print(row)
for num in row:
print(float(num))
读写文件时会遇到
iterator should return strings, not bytes (did you open the file in text mode?)
因为这个csv文件是文本文件并不是二进制文件
解决方法:
将‘rb’转换成‘r’
或者是
with open('eggs.csv','rt') as f:
reader = csv.reader(f)
next(reader,None)
for row in reader:
print(row)