Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Python prints wrong encoding from MySQL database

I'm having issues with my Python when calling data from my MySQL database.

The database is set as UTF-8 and is containing, special letters such as 'Æ' and 'Ø'.

This is the code i use to call the Usernames from my Table

# -*- coding: UTF-8 -*-
import pymysql
db = pymysql.connect(
 host="localhost",
 user="root",
 password="XXXXXXXX",
 db="pythonconnectiontest", 
 charset="utf8"
)
cursor = db.cursor()
cursor.execute("SELECT Username FROM Account")
numrows = cursor.rowcount 
for i in range (numrows):
 row = cursor.fetchone()
 print row

The expected output is:

ThisTextIsBrøken
Tæst 
Word

Actual output:

(u'ThisTextIsBr\xf8ken',)
(u'T\xe6st',)
(u'Word',)

I do get that the 'U' in front of the value, indicates that this is UNICODE and i probably just need to make python interpret the text correctly with encode() or decode().

I've spent many hours trying to figure this out, and the solution is probably very simple.

I hope someone can help me fix this.

Thanks for reading.

Answer*

Draft saved
Draft discarded
Cancel

default

AltStyle によって変換されたページ (->オリジナル) /