3
cursor.execute('SELECT userid,productid,description from goqii_store_product_rating_log')

Above is the query I am trying to read from MySQL database using python.

Here the Datatype for description column is TEXT.

When I run this I get the following error:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 8: character maps to

How can I overcome this error and read the data smoothly??.

Python Version used : 3.7.3

Regards

2
  • I think the question has already been answered here: stackoverflow.com/questions/30598350/… Commented Dec 26, 2019 at 7:02
  • @GauravAgarwal Hello Gaurav,This is basically while reading a csv file right,Here i am directly importing the data through a SQL query how do I handle it then.Can you help me with a code Snippet as I am new to Python Commented Dec 26, 2019 at 7:05

1 Answer 1

4

I think the question has already been answered here:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7240: character maps to <undefined>

This seems to be an issue that has been caused by encoding differences.

A way to work around this has been pointed out here as well. Hope this helps: How to return str from MySQL using mysql.connector?

Also, you need to provide more details while posting the question such as the connector you used (eg: PyMySQL)

Try something on these lines:

conn = pymysql.connect(host='localhost',
                       user='username',
                       passwd='password',
                       db='database',
                       charset='utf8')
Sign up to request clarification or add additional context in comments.

5 Comments

I am using MySQLdb package and MYsqldb.connect() to make the connection
When you connect() to your database, pass the charset='utf8' parameter.
Hello Gaurav,I was able to read it using the pymysql connector .Thanks a Tonne111
I did upvoted the answer but it isnt changing,Don't know why
Specifying charset="utf-8" worked for me to decode utf8mb4 encoding.

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.