Python UnicodeEncodeError

And*_*Dev 2 python mysql database python-2.7 python-unicode

我试图用这个语句将一些数据插入到MySQL数据库中:

INSERT INTO tweets(tweeter,tweet)VALUES ('Lord_Sugar','RT @BTSport: This cerebral figure succeeds in insulting everyone’s intelligence. @CalvinBook looks at Kinnears impact on #NUFC http://t.…')
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2019' in position 119: ordinal not in range(256)
Run Code Online (Sandbox Code Playgroud)

我知道它在谈论哪个角色 - 它在推文末尾的'...',但不知道如何绕过它.

fix*_*xer 5

我猜你正在使用MySQLDb进行连接.

如果是这样,则应在连接字符串中使用charset和use_unicode参数.MySQLdb默认使用Latin-1:

import MySQLdb    
dbcon = MySQLdb.connect(user="root",db="twitter",use_unicode=True,charset='UTF8')
Run Code Online (Sandbox Code Playgroud)