2

I am trying to insert some data into a MySQL Database with this statment:

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.…')

But I am getting this error:

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

I know which character its talking about - its '…' at the end of the tweet, but don't know how to get around it.

3
  • what does print(repr('...')) say the character consists of? the '...' being the odd characters. Commented Aug 19, 2013 at 15:01
  • That character isn't latin 1 sorry to say, it's something else so you need to decode it into erm 'iso-8859-15' or just run. Commented Aug 19, 2013 at 15:04
  • Python2 or Python3 btw? (makes a difference here) Commented Aug 19, 2013 at 15:06

1 Answer 1

5

I'm guessing that you are connecting using MySQLDb.

If that is so, you should use charset and use_unicode parameters in your connection string. MySQLdb by default uses Latin-1:

import MySQLdb    
dbcon = MySQLdb.connect(user="root",db="twitter",use_unicode=True,charset='UTF8')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.