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

发布于:2025-05-14 ⋅ 阅读:(12) ⋅ 点赞:(0)

报错

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

网站公告

今日签到

点亮在社区的每一天
去签到