6

I have a .txt file named "chinchars.txt". Inside, I have a single line with these two characters:

节日

How do I read this text file and return those to characters? Using this code:

inputFile = open('chinchars.txt').readlines()

It outputs this error:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 
18: character maps to <undefined>

I believe I need to "decode" the characters someway. How would this be accomplished?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Bryan Thai
  • 179
  • 1
  • 2
  • 8

1 Answers1

3

Try this, it might helps you:

inputFile = open('chinchars.txt', encoding="utf8").readlines()

Note that it is better to open a file with with. like this:

with open('chinchars.txt', encoding="utf8") as f:
    inp = f.readlines()
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
  • @DyZ yes, that is why I wrote this might help you. – Mehrdad Pedramfar Jun 23 '18 at 06:24
  • 2
    @DyZ I'm pretty sure the default for text streams (`sys.std*`, `open`) is locale/platform-dependent in Python 3. The encoding defaults to UTF-8 only for source encoding and for `str.encode/decode`. – lenz Jun 23 '18 at 06:32
  • @Elliptica, take a look at this link, it will answer you question: https://stackoverflow.com/questions/57662894/what-is-the-difference-between-read-and-readline-in-python#:~:text=The%20main%20difference%20is%20that,you%20specify%20in%20the%20parenthesis. – Mehrdad Pedramfar Apr 16 '22 at 07:03