Table of Contents

How to Fix Error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte" in Python

The below error is fixed by opening the file with right encoding...

    with open('test.csv') as fp:
 		for line in fp:
 		line = line.strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

If you know the encoding, use the codecs library to open the file as shown below...

import codecs
f = codecs.open("test.csv", "r", "utf-16")

If you don't know the encoding. Try the library chardet to detect the encoding

import chardet
chardet.detect(filedata)

Checkout my other post to know more about chardet...

https://www.usessionbuddy.com/post/how-to-read-csv-file-in-python-pandas/

Related Posts