python 执行 js 代码报错
一、报错
result = execjs.compile(js_code).call("decode", text)
python 用 execjs 执行 js 文件,语法正确,但仍报错如下:
Exception in thread Thread-3:
...
...
...
UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 2: illegal multibyte sequence
...
...
...
IndexError: list index out of range
Process finished with exit code 1
二、解决
找到execjs模块下的_external_runtime.py文件,里面有一句代码:
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=self._cwd, universal_newlines=True)
添加 encoding =‘utf-8’,如下:
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=self._cwd, universal_newlines=True, encoding='utf-8')
三、原文链接
https://blog.csdn.net/u012552769/article/details/104290493?spm=1001.2014.3001.5502