【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 method
    original_read_text = pathlib.Path.read_text
    
    # Create patched version that defaults to utf-8
    def 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 patch
    pathlib.Path.read_text = patched_read_text

if __name__ == "__main__":
    force_utf8()
    
    # Run the original script
    if 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等风来不如迎风去

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值