Python3 reads utf8 files

Phanix
·
·
IPFS
·

For the record, forget about Orz every time

The default is not to read files in utf8, so an error message like this will be sprayed

File “/usr/lib/python3.6/encodings/ascii.py”, line 26, in decode
return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 29: ordinal not in range(128)

The solution is to add encoding=”utf-8” when opening the file.

 with open(os.path.join("./", inputfile), "r", encoding="utf-8") as my_file:
    tree_text = my_file.read()

Original link: Phanix's Blog

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!