# UnicodeEncodeError：'ascii' 编解码器无法编码位置 10 处的字符 '\u2019'：序数不在 128 范围内

- **ID:** `python/sqlalchemy-unicode-encode-error`
- **领域:** python
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Python 2 或配置错误的 Python 3 环境尝试编码非 ASCII 字符。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Ensure database connection uses UTF-8.** (95% 成功率)
   ```
   engine = create_engine('postgresql://user:pass@localhost/dbname?client_encoding=utf8')
   ```
2. **Encode strings to UTF-8 before storing.** (90% 成功率)
   ```
   name = 'José'.encode('utf-8').decode('utf-8')  # Ensure proper encoding
   ```

## 无效尝试

- **Replacing characters with ASCII equivalents manually.** — Not scalable; data loss. (60% 失败率)
- **Setting sys.setdefaultencoding('utf-8') in Python 2.** — Deprecated and may cause side effects. (70% 失败率)
