-2

so guys i just copied a couple of paragraph into a text file (test.txt) and ran this

with open('test.txt','r') as f:
   print(f.read())

and i got this

 return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1588: character maps to <undefined>

i dont know what that error means , i dont know why it wont work ,its just a simple code , so can someone explain what is the problem in simple words. Thanks

shady
  • 33
  • 5

2 Answers2

3

Try add encoding in the open statement:

with open('test.txt','r', encoding='utf-8') as f:
   print(f.read())
anyryg
  • 313
  • 2
  • 13
0

In my experience, this has occurred on Windows when trying to read a file with emojis

Try reading the raw binary form of the file as such:

with open('text3.txt', 'rb') as f:
    print(f.read())
Jarrod
  • 21
  • 4