# CSV解析错误：引号字符不匹配 — 期望 '"' 但找到 ''

- **ID:** `data/csv-quote-escape-mismatch`
- **领域:** data
- **类别:** encoding_error
- **错误码:** `CSVParseException`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

CSV文件使用单引号引用字段，但解析器期望双引号，反之亦然，通常由于区域设置或导出设置导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| python 3.12 | active | — | — |
| pandas 2.2.0 | active | — | — |
| apache-commons-csv 1.10.0 | active | — | — |

## 解决方案

1. ```
   Specify the correct quote character in pandas: `pd.read_csv('file.csv', quotechar="'")` if the file uses single quotes.
   ```
2. ```
   Use the `escapechar` parameter if quotes are escaped with backslash: `pd.read_csv('file.csv', escapechar='\\')`.
   ```
3. ```
   Preprocess the file with a Python script to normalize quotes: `import csv; with open('input.csv', 'r') as f, open('output.csv', 'w', newline='') as out: reader = csv.reader(f, quotechar="'"); writer = csv.writer(out, quotechar='"'); writer.writerows(reader)`
   ```

## 无效尝试

- **Manually replacing all single quotes with double quotes in the CSV file using sed** — This can corrupt data if single quotes are part of the field content (e.g., names like O'Brien). (60% 失败率)
- **Ignoring the error and proceeding with partially parsed data** — Results in misaligned columns and corrupt data; downstream processes will fail or produce wrong results. (95% 失败率)
- **Specifying quote character in parser but using the wrong escape character** — If the escape character is also wrong (e.g., backslash vs doubling), parsing will still fail on embedded quotes. (70% 失败率)
