python读取csv文件是报错_csv.Error: iterator should return strings, not bytes (did you open the file in text

python 读取csv文件报错问题

import csv

with open('E:/Selenium2script/DDT模块/test.csv','rb') as f:
    readers = csv.reader(f)
    next(readers,None)
    for line in readers:
        print(line)

输出:
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)


问题分析:因为此csv文件是一个文本文件,并非二进制文件。


解决:

import csv

with open('E:/Selenium2script/DDT模块/test.csv','rt') as f:
    readers = csv.reader(f)
    next(readers,None)
    for line in readers:
        print(line)
或者‘rt’换成‘r’


备注:此处的next语句为不读取标题key值


with open(file_name,'wt',newline='') as f:

加入newline=' ',写入的数据不会空行。

你可能感兴趣的:(python,python)