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'
后,程序可正常运行