1

In MySQL I have a field with the value: "Rœschwoog"

The charset of the MySQL table is UTF-8. When I retrieve that value from Python 3.4, I save it into a dict. When I print that dict, I realize that the value is not "Rœschwoog" but "R\x9cschwoog" instead. Why is this happening? How can I get the real value?

I create the MySQL connection with:

conn = pymysql.connect(host='127.0.0.1', user='xxx', passwd="yyy", db='mydb')
Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
alvaro.rmfg
  • 379
  • 3
  • 15
  • `"R\x9cschwoog"` is Python's *representation* of the string `Rœschwoog` encoded in the `windows-1252` encoding. Can you please add the relevant parts of your code (a simplified testcase) to your question? Particularly, what connection parameters do you use when creating your MySQL connection? – Lukas Graf Sep 15 '15 at 19:11
  • 1
    Thx for your answer. I edited my previous question with the mysql connection info. – alvaro.rmfg Sep 15 '15 at 19:15
  • 1
    Try adding `charset="utf8"` to your connection parameters. See [this answer](http://stackoverflow.com/a/8365782/1599111), [this one](http://stackoverflow.com/a/9155014/1599111) and the [pymsql docs](https://github.com/PyMySQL/PyMySQL) for some more details. – Lukas Graf Sep 15 '15 at 19:20
  • Gread, glad I could help. I'll therefore flag your question with the appropriate duplicate, if I think that's correct you should be able to confirm that and immediately mark it as a dupe. – Lukas Graf Sep 15 '15 at 19:41

1 Answers1

0

Not charset=utf8, you need to specify that your bytes are encoded cp1252, or perhaps latin1 (They are similar.)

Rick James
  • 135,179
  • 13
  • 127
  • 222