【发布时间】:2012-09-20 03:08:22
【问题描述】:
我需要(在 Python 中)将 4 字节字符转换为其他字符。这是将其插入到我的 utf-8 mysql 数据库中而不会出现错误,例如:“Incorrect string value: '\xF0\x9F\x94\x8E' for column 'line' at row 1”
Warning raised by inserting 4-byte unicode to mysql 显示这样做:
>>> import re
>>> highpoints = re.compile(u'[\U00010000-\U0010ffff]')
>>> example = u'Some example text with a sleepy face: \U0001f62a'
>>> highpoints.sub(u'', example)
u'Some example text with a sleepy face: '
但是,我在评论中遇到与用户相同的错误,“...错误的字符范围..”这显然是因为我的 Python 是 UCS-2(而不是 UCS-4)构建。但后来我不清楚该怎么做?
【问题讨论】:
-
在MySql中使用utf8mb4 charset还会有问题吗?
-
不确定。不幸的是,我无法更改数据库的字符集。
标签: python mysql utf-8 character-encoding python-unicode