紀錄一下,每次都忘記 Orz
預設的話不是 utf8 讀檔案,所以會噴類似這樣的錯誤訊息
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)
解法就是開啟檔案時加上encoding=”utf-8″。
with open(os.path.join("./", inputfile), "r", encoding="utf-8") as my_file: tree_text = my_file.read()