UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 38619: character maps to <undefined> code example
Example 1: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 6148: character maps to
Add encoding:
file = open(filename, encoding="utf8")
Example 2: 'charmap' codec can't decode byte 0x98 in position
file = open(filename, encoding="utf8")
Example 3: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 92: character maps to
file = open(filename, encoding="cp437")
Example 4: UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 550: character maps to
#Filename Read_file.py
#Author Steve Morse
#Date 5/30/2020
# Cleaned up 9/30/2020
x=" "
# process_lines DID NOT WORK AT FIRST
# I had fogotten that the EOL (end of line) charcter/values were
# still attached to the line
# .strip() got rid of them, no problem
# All this does is print out the line.
def process_lines(line, count):
x=""
print(count)
#print(line)
line2=line.strip() # This strips the offending EOL chars
#print(line2)
# This is my code for finishing the loop
if line2=='Order Number:':
x=''
print(line2)
print('xxxxxxxxxxxxxxxxxxxxxx')
x=input('Enter to continue, X to finish')
if x=='x':
return 'x'
if x=='X':
return 'x'
print("\n\n")
return x
#=========================================================
Main Routine
#Orders is a BIG file encoded UTF-8
#Orders1 is a SMALL file encoded UTF-8 (1st 5 entries of Orders)
#Initially I had an error
# 'utf-8' codec can't decode byte 0xe2 : invalid continuation byte error
#
# This was solver with the following:
# encoding="UTF-8"
counter=0
with open('orders1.txt', 'r',encoding="UTF-8") as f:
for line in f:
counter +=1
xx=process_lines(line, counter)
if xx=='x':
break
#=========================================================
# So I know it has finished
print("\n\n\n========================================\nFinished")