問題描述
運行 Tensorflow2 升級腳本會觸發編碼錯誤 (Running the Tensorflow2 upgrade Script triggers an encoding error)
我目前正在嘗試使用 https://www.tensorflow.org/guide/upgrade,這樣我就可以在 google colab 中運行它,但是當我運行這個單元格時(來自 Windows 中的 Anaconda 環境) :
!tf_upgrade_v2 \
‑‑infile D:/Multiclass.py\
‑‑outfile /tmp/upgraded_Multiclass_CNN_V01_31012020Copy.py
我得到一個編碼錯誤:
Traceback (most recent call last):
File "d:\programdata\miniconda3\envs\env_dlexp1\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\programdata\miniconda3\envs\env_dlexp1\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\ProgramData\Miniconda3\envs\Env_DLexp1\Scripts\tf_upgrade_v2.exe\__main__.py", line 7, in <module>
File "C:\Users\user\AppData\Roaming\Python\Python37\site‑packages\tensorflow_core\tools\compatibility\tf_upgrade_v2_main.py", line 139, in main
args.input_file, output_file, upgrade)
File "C:\Users\user\AppData\Roaming\Python\Python37\site‑packages\tensorflow_core\tools\compatibility\tf_upgrade_v2_main.py", line 40, in process_file
upgrader.process_file(in_filename, out_filename)
File "C:\Users\user\AppData\Roaming\Python\Python37\site‑packages\tensorflow_core\tools\compatibility\ast_edits.py", line 900, in process_file
temp_file)
File "C:\Users\user\AppData\Roaming\Python\Python37\site‑packages\tensorflow_core\tools\compatibility\ast_edits.py", line 958, in process_opened_file
lines = in_file.readlines()
File "d:\programdata\miniconda3\envs\env_dlexp1\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 14: character maps to <undefined>
我通常會通過這樣做將編碼更改為utf‑8
: open(path, mode, encoding='UTF8')
,但在這種情況下我一無所知。我不是程序員,所以我的能力非常有限。請幫助我理解這個錯誤。
參考解法
方法 1:
Try editing with open
in line 918 of function process_file
in ast_edits.py and specify encoding='utf‑8', so that in the function process_opened_file
the file can be read with utf‑8.
(by Eric Avila Torres、Greensky Woodson)