You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提交代码时Pylint触发UnicodeEncodeError报错,求解决方案

Pylint UnicodeEncodeError 解决方法

提交代码时使用Pylint检查,触发以下编码错误导致失败:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 1699-1713: character maps to

报错回溯信息:

Traceback (most recent call last):
  File "\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "\venv\tso_ingestion\Scripts\pylint.EXE\__main__.py", line 7, in <module>
  File "\venv\tso_ingestion\lib\site-packages\pylint\__init__.py", line 36, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "\venv\tso_ingestion\lib\site-packages\pylint\lint\run.py", line 213, in __init__
    linter.check(args)
  File "\venv\tso_ingestion\lib\site-packages\pylint\lint\pylinter.py", line 701, in check
    with self._astroid_module_checker() as check_astroid_module:
  File "\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 142, in __exit__
    next(self.gen)
  File "\venv\tso_ingestion\lib\site-packages\pylint\lint\pylinter.py", line 1010, in _astroid_module_checker
    checker.close()
  File "\venv\tso_ingestion\lib\site-packages\pylint\checkers\similar.py", line 875, in close
    self.add_message("R0801", args=(len(couples), "\n".join(msg)))
  File "\venv\tso_ingestion\lib\site-packages\pylint\checkers\base_checker.py", line 164, in add_message
    self.linter.add_message(
  File "\venv\tso_ingestion\lib\site-packages\pylint\lint\pylinter.py", line 1323, in add_message
    self._add_one_message(
  File "\venv\tso_ingestion\lib\site-packages\pylint\lint\pylinter.py", line 1281, in _add_one_message
    self.reporter.handle_message(
  File "\venv\tso_ingestion\lib\site-packages\pylint\reporters\text.py", line 208, in handle_message
    self.write_message(msg)
  File "\venv\tso_ingestion\lib\site-packages\pylint\reporters\text.py", line 201, in write_message
    self.writeln(self._fixed_template.format(**self_dict))
  File "\venv\tso_ingestion\lib\site-packages\pylint\reporters\base_reporter.py", line 64, in writeln
    print(string, file=self.out)
  File "\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1699-1713: character maps to <undefined>

唯一代码改动为断言重构:
原代码:

assert e_info.type is PageException
    assert e_info.value.args[0].url == url
    assert e_info.value.args[0].body == soup.body
    assert e_info.value.args[0].element == soup.body.find("form", id="form").find(
        "a", string="料金通知情報一覧"
    )
    assert (
        "Onclick event associated with \\'料金通知情報一覧\\' link was missing or malformed"
        in str(e_info.value.args[0])
    )

重构后代码:

check_page_exception(
        e_info,
        url,
        soup.body,
        soup.body.find("form", id="form").find("a", string="料金通知情報一覧"),
        "Onclick event associated with \\'料金通知情報一覧\\' link was missing or malformed",
    )

问题原因

Windows默认控制台编码为CP1252,无法处理日文Unicode字符。重构后代码触发了Pylint的相似性检查(R0801),该模块输出包含日文的内容时,控制台编码不兼容导致编码失败。

解决方法

1. 指定Pylint输出编码为UTF-8

运行Pylint时添加编码参数:

pylint --output-encoding=utf-8 your_module.py

或者在项目的.pylintrc配置文件中添加:

[REPORTS]
output-encoding = utf-8

2. 切换控制台编码为UTF-8

运行Pylint前执行命令切换编码:

chcp 65001

3. 临时禁用相似性检查规则

如果不需要R0801检查,可临时禁用该规则:

pylint --disable=R0801 your_module.py

内容的提问来源于stack exchange,提问作者Woody1193

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 14:52:53