# 使用Windows-1252编码的CSV文件在按UTF-8读取时导致乱码

- **ID:** `data/csv-encoding-mismatch-windows-1252`
- **领域:** data
- **类别:** encoding_error
- **错误码:** `CSV_ENCODING_WINDOWS_1252`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

CSV文件使用Windows-1252编码保存（在旧系统中常见），但读取器假定为UTF-8，导致对重音字母等字符的字节错误解释。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Python 3.10 | active | — | — |
| pandas 2.0.0 | active | — | — |
| Microsoft Excel 2019 | active | — | — |

## 解决方案

1. ```
   Use Python's `chardet` library to detect encoding and then read with the correct encoding: `import chardet; with open('file.csv', 'rb') as f: result = chardet.detect(f.read()); df = pd.read_csv('file.csv', encoding=result['encoding'])`.
   ```
2. ```
   Convert the file to UTF-8 using iconv: `iconv -f WINDOWS-1252 -t UTF-8 input.csv > output.csv`.
   ```

## 无效尝试

- **** — Adding UTF-8 BOM to the file does not fix the encoding; it only helps with byte order marking but does not convert the actual character encoding. (90% 失败率)
- **** — Manually replacing garbled characters one by one is error-prone and does not scale for large files. (80% 失败率)
