MariaDB / Python issues with emoji characters

帅比萌擦擦* 提交于 2019-12-13 15:53:51

问题


I have a string in x['foo'], sometimes it's a normal string, other times it is a emoji or text art ..

So, I am getting this error in trying to submit the content into database.

Warning: Incorrect string value: '\xE4\xB8\xBA Co...' for column 'description' at row 1

If I use x['foo'].decode("utf-8") I get this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u4e3a' in position 0: ordinal not in range(128)

If I try, encode('ascii', 'ignore').decode('ascii') Then I get this error.

x['foo'].encode('ascii', 'ignore').decode('ascii') AttributeError: 'NoneType' object has no attribute 'encode'

I am using MariaDB 5.5.44 and Python 2.7.5

I am initializing my database as

conn = MySQLdb.connect("localhost","root","","")
conn.set_character_set('utf8')
cursor = conn.cursor()
cursor.execute('SET NAMES utf8;')
cursor.execute('SET CHARACTER SET utf8;')
cursor.execute('SET character_set_connection=utf8;')

来源:https://stackoverflow.com/questions/32994772/mariadb-python-issues-with-emoji-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!