问题
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