2
#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import MySQLdb

conn = MySQLdb.connect('localhost', 'django_user', 'haha123', 'mydb')
cur = conn.cursor()

f = open('/home/kave/projects/cb/database/country_code_drupal_nov_2011.txt')

cur.execute("INSERT INTO myapp_app_currency (currency) VALUES ('USD - $'),('EUR - €'), ('GBP - £'), ('CAD - $'), ('AUD - $'), ('BRL - R$');")

For some reason I can insert this data successfully into my database, however two entries get corrupted.

EUR - € becomes EUR - €
GBP - £ becomes GBP - £

I thought I had set it as utf8 and it should roll, why there a problem only with those two characters?

1
  • Incidentally, you should probably split the symbol and the description into separate columns. Commented Nov 10, 2012 at 0:01

1 Answer 1

3

Try setting the charset when connecting to the database:

conn = MySQLdb.connect('host', 'usr', 'pass', 'db', charset='utf8')

When you use # -*- coding: utf-8 -*- in the document, it only applies to the source code document, not to what it performs.

Sign up to request clarification or add additional context in comments.

2 Comments

that worked. Thanks. Could you elaborate what you mean by actual source?
I mean that it specifies the encoding of the source code document. See PEP 0263 for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.