python reads csv file is an error _csv.Error: iterator should return strings, not bytes (did you open the file in text)

Python reads the CSV file and reports an error

import csv

with open('E:/Selenium2script/DDTModle/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?)

Problem analysis: This CSV file is a text file, not a binary file.

Solution:

import csv

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

Or we could replace rt with R.

Note: The next statement here does not read the header key value

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

Add newline= “‘, the data written will not be blank.

Read More: