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

Word2Vec训练前遍历目录遇Unicode解码错误,求原因排查

本地运行Word2Vec目录遍历代码时的UnicodeDecodeError问题诊断

你遇到的UnicodeDecodeError提示无法解码0x90字节,即便确认文件是UTF-8编码仍未解决,结合代码在Hathi Trust环境正常、本地报错的情况,以下是可能的原因及对应解决方案:

1. 文件实际编码为Windows-1252而非UTF-8

0x90字节是Windows-1252编码的控制字符,Hathi Trust基于类Unix系统,默认编码与Windows差异大,因此之前未触发错误。你所谓的"确保UTF-8编码"可能存在偏差,比如文件是从Windows系统导出的,实际编码为Windows-1252。

解决办法:

  • 直接尝试用Windows-1252编码打开文件:
    with open(os.path.join(base_dir, doc), "r", encoding="windows-1252") as file:
    
  • 用chardet库自动检测文件编码:
    先安装依赖:
    pip install chardet
    
    修改代码:
    import chardet
    
    for doc in docs:
        if not doc.startswith('.'):
            file_path = os.path.join(base_dir, doc)
            # 二进制读取检测编码
            with open(file_path, 'rb') as f:
                detect_result = chardet.detect(f.read())
            # 使用检测到的编码读取
            with open(file_path, 'r', encoding=detect_result['encoding']) as file:
                text = file.read()
                all_docs.append(text)
    

2. 路径拼接错误导致读取非目标文件

代码中base_dir + doc的拼接方式存在隐患:如果base_dir末尾没有路径分隔符(如/或\),会生成错误路径(比如datatest.txt而非data/test.txt),可能读取到二进制文件或无关文件,引发解码错误。

解决办法:
用os.path.join()或pathlib.Path安全拼接路径:

from pathlib import Path

base_dir = Path("redacting directory name for the purposes of this question")
all_docs = []

# 遍历目录下的文件,自动处理路径
for doc_path in base_dir.iterdir():
    if not doc_path.name.startswith('.') and doc_path.is_file():
        with open(doc_path, "r", encoding="utf-8") as file:
            text = file.read()
            all_docs.append(text)

3. 文件混入二进制数据

部分文件可能因损坏、传输异常等混入二进制内容,表面显示为UTF-8,但实际包含无法解码的字节。

解决办法:
读取文件时添加错误处理参数,跳过或替换无效字符:

# 替换无效字符为�
with open(os.path.join(base_dir, doc), "r", encoding="utf-8", errors="replace") as file:
# 或直接忽略无效字符
with open(os.path.join(base_dir, doc), "r", encoding="utf-8", errors="ignore") as file:

4. 本地Python环境编码差异

Hathi Trust的Python环境默认编码为UTF-8,而Windows本地Python默认编码通常为GBK,导致文件读取时编码处理逻辑不一致。

解决办法:

  • Python 3中可通过设置环境变量PYTHONUTF8=1强制Python使用UTF-8编码;
  • 或在代码开头显式指定文件读取的编码(即前面提到的指定正确编码)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 15:00:05