[Solved] UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbf in position 7: invalid start byte

The code is as follows:

f = csv.reader(open(csvroot, 'r',encoding='utf-8'))
for i in f:
    print(i)

Solution: view the file encoding format

import chardet

f = open(full_csvroot, 'rb')
data = f.read()
print(chardet.detect(data))

# Outcome:
# {'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}

Amend to read:

f = csv.reader(open(csvroot, 'r',encoding='GB2312'))
for i in f:
    print(i)

Perfect operation

Read More: