相关疑难解决方法(0)

通过向mysql插入4字节unicode引发警告

请看以下内容:

/home/kinka/workspace/py/tutorial/tutorial/pipelines.py:33: Warning: Incorrect string 
value: '\xF0\x9F\x91\x8A\xF0\x9F...' for column 't_content' at row 1
n = self.cursor.execute(self.sql, (item['topic'], item['url'], item['content']))
Run Code Online (Sandbox Code Playgroud)

字符串'\xF0\x9F\x91\x8A,实际上是一个4字节的unicode : u'\U0001f62a'. mysql的字符集是utf-8但是插入4字节的unicode会截断插入的字符串.我搜索了这个问题,发现5.5.3下的mysql不支持4字节unicode,不幸的是我的是5.5.224.我不想升级mysql服务器,所以我只想过滤python中的4字节unicode,我尝试使用正则表达式但失败了.那么,有什么帮助吗?

python regex mysql astral-plane

7
推荐指数
1
解决办法
3568
查看次数

Python,转换4字节字符以避免MySQL错误"字符串值不正确:"

我需要将(在Python中)一个4字节的char转换为其他字符.这是将其插入到我的UTF-8 mysql数据库没有得到一个错误,如:"不正确的字符串值:在第1行'\ XF0\x9F\X94\x8E’列'线’"

通过向mysql插入4字节unicode引发的警告显示这样做:

>>> 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: '
Run Code Online (Sandbox Code Playgroud)

但是,我得到了与评论中的用户相同的错误,"...字符范围很差.."这显然是因为我的Python是UCS-2(而不是UCS-4)版本.但后来我不知道该怎么做呢?

python mysql utf-8 character-encoding python-unicode

7
推荐指数
1
解决办法
3520
查看次数