1. 程式人生 > 其它 >Python3 執行 JS 遇到編碼錯誤

Python3 執行 JS 遇到編碼錯誤

技術標籤:踩坑pythonwindows

Python3 執行 JS 遇到編碼錯誤

Python3使用import execjs執行 JS 程式碼,

遇到如下報錯,測試用 JS 直接執行是沒有問題的,也就是說 JS 程式碼沒有問題,問題出在檔案編碼上

UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 14769: illegal multibyte sequence
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\execjs\_abstract_runtime_context.py", line 37, in call
    return self._call(name, *args)
  File "C:\Python39\lib\site-packages\execjs\_external_runtime.py", line 92, in _call
    return self._eval("{identifier}.apply(this, {args})".format(identifier=identifier, args=args))
  File "C:\Python39\lib\site-packages\execjs\_external_runtime.py", line 78, in _eval
    return self.exec_(code)
  File "C:\Python39\lib\site-packages\execjs\_abstract_runtime_context.py", line 18, in exec_
    return self._exec_(source)
  File "C:\Python39\lib\site-packages\execjs\_external_runtime.py", line 87, in _exec_
    output = self._exec_with_pipe(source)
  File "C:\Python39\lib\site-packages\execjs\_external_runtime.py", line 103, in _exec_with_pipe
    stdoutdata, stderrdata = p.communicate(input=input)
  File "f:\WorkSpace\PythonProject\sojson_decrypt\subprocess.py", line 1130, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "f:\WorkSpace\PythonProject\sojson_decrypt\subprocess.py", line 1525, in _communicate
    stdout = stdout[0]

跟進subprocess.py這個模組搜尋encoding=關鍵字,修改值為'utf-8'後,程式可正常執行

在這裡插入圖片描述