当前位置: 首页 > news >正文

【python】UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xb2

报错

C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\python.exe D:\XTRANS\cuda\03-graph-db\04-cmkg\pdf2zh-v1.9.9-with-assets-win64\pdf2zh\gui.py Traceback (most recent call last): File “D:\XTRANS\cuda\03-graph-db\04-cmkg\pdf2zh-v1.9.9-with-assets-win64\pdf2zh\gui.py”, line 10, in import gradio as gr File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio_init_.py”, line 3, in import gradio.simple_templates File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio_simple_templates_init.py”, line 1, in from .simpledropdown import SimpleDropdown File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio_simple_templates\simpledropdown.py”, line 6, in from gradio.components.base import FormComponent File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio\components_init_.py”, line 40, in from gradio.components.multimodal_textbox import MultimodalTextbox File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio\components\multimodal_textbox.py”, line 28, in class MultimodalTextbox(FormComponent): File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio\component_meta.py”, line 198, in new create_or_modify_pyi(component_class, name, events) File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\site-packages\gradio\component_meta.py”, line 92, in create_or_modify_pyi source_code = source_file.read_text() File “C:\Users\zhangbin\AppData\Local\Programs\Python\Python310\lib\pathlib.py”, line 1135, in read_text return f.read() UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xb2 in position 1972: illegal multibyte sequence

分析

  • The error shows a character encoding issue when Python tries to read files using the system’s default encoding (GBK) on your Windows system. This is a common problem when working with packages that contain non-ASCII characters.

Here’s how you can fix it:

# Create a file named fix_encoding.py with this content
import sys
import locale
import pathlib# Force UTF-8 as default encoding
def force_utf8():# Save original read_text methodoriginal_read_text = pathlib.Path.read_text# Create patched version that defaults to utf-8def patched_read_text(self, encoding=None, errors=None):if encoding is None:encoding = 'utf-8'return original_read_text(self, encoding=encoding, errors=errors)# Apply the patchpathlib.Path.read_text = patched_read_textif __name__ == "__main__":force_utf8()# Run the original scriptif len(sys.argv) > 1:exec(open(sys.argv[1], encoding='utf-8').read())

执行修复

python fix_encoding.py D:\XTRANS\cuda\03-graph-db\04-cmkg\pdf2zh-v1.9.9-with-assets-win64\pdf2zh\gui.py

或者:设置环境

  • Alternatively, you can modify your environment by setting the PYTHONIOENCODING environment variable:
  • This forces Python to use UTF-8 encoding for file operations.
set PYTHONIOENCODING=utf-8
python D:\XTRANS\cuda\03-graph-db\04-cmkg\pdf2zh-v1.9.9-with-assets-win64\pdf2zh\gui.py
http://www.xdnf.cn/news/451675.html

相关文章:

  • 一种资源有限单片机处理cJSON数据的方法
  • 编写第一个MCP Client之Hello world
  • Android RTL语言视图适配(保加利亚,阿拉伯语种等)
  • JAVA中的文件操作
  • sqli—labs第六关——双引号报错注入
  • BitMart合约交易体验 BitMart滑点全赔的底层逻辑
  • 朱老师,3518系列,第八季
  • 使用Git+Cron实现BIND的Named域名配置自动化管理!
  • D2203使用手册—高压、小电流LDO产品4.6V~36V、150mA
  • AD 异性铺铜
  • 破解商业综合体清洁管理困局:商业空间AI智能保洁管理系统全场景解决方案
  • CodeBuddy 接入 MCP,一键生成网站!
  • 从规则驱动到深度学习:自然语言生成的进化之路
  • SpringbBoot nginx代理获取用户真实IP
  • 3337|3335. 字符串转换后的长度 I(||)
  • leetcode2934. 最大化数组末位元素的最少操作次数-medium
  • 练习小项目:倒数日小工具
  • Windows软件插件-写wav
  • 【Embedding Models】嵌入模型选择指南
  • 蓝卓入选2025宁波最具潜力新品牌TOP10
  • 数据库字段唯一性修复指南:从设计缺陷到规范实现
  • 安装windows版本的nacos
  • 总结下Jackson 中的JsonNode,ObjectNode,ArrayNode的方法
  • 时代推动建筑管理变革,楼宇自控系统成现代建筑管理必由之路
  • 数据结构·字典树
  • 每周靶点:TREM2、DLL3及文献分享
  • 代码随想录算法训练营第60期第三十六天打卡
  • W1电力线载波通信技术
  • Linux 常用命令 -hostnamectl【主机名控制】
  • Mixup