python decode UnicodeDecodeError: 'utf-8' codec can't decode byte invalid start byte code example

Example 1: UnicodeDecodeError: 'utf-8' codec can't decode byte invalid start byte

# Use 'ISO-8859-1' instead of "utf-8" for decoding
text = open(fn, 'rb').read().decode('ISO-8859-1')

Example 2: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

with open(file, newline='', encoding="utf16") as MyFile:

Example 3: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 10: invalid start byte

#use rb over r
with open(path, 'rb') as f:
  text = f.read()

Tags:

Misc Example