社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python 运行 execjs 出现错误 UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\ufffd‘

梦想橡皮擦 • 4 年前 • 431 次点击  

异常解读

该异常的格式一般为:

UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\ufffd’ in position 160: illegal multibyte sequence

该异常出现的场景为在 Windows 电脑下使用 Python execjs 运行指定的 JS 文件,但 JS 文件中包含 中文

异常解决方案

该问题解决需要 修改 你电脑 Python 安装目录下 lib 文件夹里面的一个文件,名字叫做 subprocess.py

例如在我本地为 :
python 运行 execjs 出现错误 UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\ufffd‘
通过任意文本编辑器打开该文件,在文件中检索 encoding ,找到如下位置:
python 运行 execjs 出现错误 UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\ufffd‘
修改 encoding=None encoding = "utf-8"

代码位置:

    def __init__(self, args, bufsize=-1, executable=None,
                 stdin=None, stdout=None, stderr=None,
                 preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
                 shell=False, cwd=None, env=None, universal_newlines=False,
                 startupinfo=None, creationflags=0,
                 restore_signals=True, start_new_session=False,
                 pass_fds=(), *, encoding="utf-8", errors=None):
        """Create new Popen instance."""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

保存文件,解决问题。

附录

本系列文章只供记录 Python 日常开发过程中 偶遇 BUG,提供给学生作为参考数据与解决方案用,属于 记录型博客 ,有缘分看到的读者希望可解决你的问题。

错误提示样板,可以与你的错误进行比对。

Traceback (most recent call last):
  File "g:/MyFiles/xiaoke/a.py", line 38, in <module>
    zh("")
  File "g:/MyFiles/xiaoke/a.py", line 19, in zh
    obj = docjs.eval('obj')
  File "F:\P3.6\lib\site-packages\execjs\_abstract_runtime_context.py", line 27, in eval
    return self._eval(source)
  File "F:\P3.6\lib\site-packages\execjs\_external_runtime.py", line 78, in _eval
    return self.exec_(code)
  File "F:\P3.6\lib\site-packages\execjs\_abstract_runtime_context.py", line 18, in exec_
    return self._exec_(source)
  File "F:\P3.6\lib\site-packages\execjs\_external_runtime.py", line 87, in _exec_
    output = self._exec_with_pipe(source)
  File "F:\P3.6\lib\site-packages\execjs\_external_runtime.py", line 103, in _exec_with_pipe
    stdoutdata, stderrdata = p.communicate(input=input)
  File "F:\P3.6\lib\subprocess.py", line 863


    
, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "F:\P3.6\lib\subprocess.py", line 1106, in _communicate
    self._stdin_write(input)
  File "F:\P3.6\lib\subprocess.py", line 801, in _stdin_write
    self.stdin.write(input)
UnicodeEncodeError: 'gbk' codec can't encode character '\ufffd' in position 160: illegal multibyte sequence

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

爬虫 100 例课程宣传

151只宝可梦(神奇宝贝)倒背的我,却连元素周期表都背不过 -- Python 爬虫小课 3-9
爬虫百例教程导航链接 : https://blog.csdn.net/hihell/article/details/86106916

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/98966
 
431 次点击