理解python数据编码问题
我正在通过请求从facebook获取一些数据。这是示例数据理解python数据编码问题,python,python-3.x,character-encoding,Python,Python 3.x,Character Encoding,我正在通过请求从facebook获取一些数据。这是示例数据 response = {'message': 'I have recommended your name to all my family n friend s. Thankyou!!!!\\ud83d\\ude0a\\ud83d\\ude0a\\ud83e\\udd17\\ud83e\\udd17\\ud83d\\udc4c\\ud83d\\udc4c\\ud83d\\udc4d\\ud83d\\udc4 } 最后几个字符是表情符
response = {'message': 'I have recommended your name to all my family n friend
s. Thankyou!!!!\\ud83d\\ude0a\\ud83d\\ude0a\\ud83e\\udd17\\ud83e\\udd17\\ud83d\\udc4c\\ud83d\\udc4c\\ud83d\\udc4d\\ud83d\\udc4
}
最后几个字符是表情符号。但当我需要将它保存在数据库中时
因此,我首先尝试将其转换为一个单词,以便添加键和操作数据:
response = json.loads(response.content, encoding='utf-8')
但是当我打印(应答)时,我得到的是
{
'message': 'I have recommended your name to all my family n friend
s. Thankyou!!!!__ __ __ __ __ __ __
}
从db我得到这个错误:
Incorrect string value: '\xF0\x9F\x98\x8A\xF0\x9F...'
我得到的编码是什么?如何转换它,以便将其存储在数据库(mysql)这是unicode。存储时必须对字符串进行解码,打印时必须进行编码。您可以使用Unicode数据:
title = u"Klüft skräms inför på fédéral électoral große"
import unicodedata
unicodedata.normalize('NFKD', title).encode('ascii','ignore')
'Kluft skrams infor pa federal electoral groe'
或者用您自己指定的字符替换这些字符,以便以后用作表情符号:
>>> a=u"aaaàçççñññ"
>>> type(a)
<type 'unicode'>
>>> a.encode('ascii','ignore')
'aaa'
>>> a.encode('ascii','replace')
'aaa???????'
>>>
>>> s= u'£10'
>>> s.encode('utf8')
'\xc2\x9c10'
>>> s.encode('utf16')
'\xff\xfe\x9c\x001\x000\x00'