data encoding_error ai_generated true

CSV文件使用UTF-8编码,但在期望Latin-1编码的工具(如Excel)中打开时显示为乱码

CSV file with UTF-8 encoded data displays as garbled text when opened in Excel or other tools expecting Latin-1

ID: data/csv-encoding-mismatch-latin1-vs-utf8

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2023-03-15首次发现

版本兼容性

版本状态引入弃用备注
pandas 1.5.0 active
Python 3.10 active
Excel 365 active
LibreOffice 7.4 active

根因分析

CSV文件以UTF-8编码保存,但读取器或应用程序(如Excel)默认使用Latin-1(ISO 8859-1)编码,导致扩展字符被错误解释。

English

CSV files are saved with UTF-8 encoding but the reader or application (e.g., Excel) defaults to Latin-1 (ISO 8859-1), causing extended characters to be misinterpreted.

generic

解决方案

  1. 使用Python重新保存CSV文件并显式指定UTF-8编码:df.to_csv('file.csv', encoding='utf-8-sig'),添加BOM以兼容Excel。
  2. 在文本编辑器中打开CSV文件,然后使用'另存为'功能选择UTF-8编码。在Excel中,使用数据>从文本/CSV导入,并选择'65001: Unicode (UTF-8)'作为文件来源。

无效尝试

常见但无效的做法:

  1. Manually changing file extension to .txt and re-importing 90% 失败

    Does not address the underlying encoding issue; the data remains corrupted during import.

  2. Using a hex editor to remove the UTF-8 BOM 70% 失败

    Removing BOM only affects byte order mark, not the actual encoding; data still garbled if reader assumes Latin-1.