LiQ
LiQ

Reputation: 1

Script crashes when trying to read a specific Japanese character from file

I was trying to save some Japanese characters from a text file into a string. Most of the characters like "道" make no problems. But others like "坂" don't work. When I'm trying to read them, my script crashes. Do I have to use a specific encoding while reading the file?

That's my code btw:

with open(path, 'r') as file:
    lines = [line.rstrip() for line in file]

The error is:

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

Upvotes: 0

Views: 280

Answers (1)

Pw Wolf
Pw Wolf

Reputation: 350

You have to specify the encoding when working with non ASCII, like this:

file = open(filename, encoding="utf8")

Upvotes: 1

Related Questions