0

I'm going to insert data to a table in mysql:

cursor.execute('INSERT INTO master_product(category_Id, manufacture_Id, asin, title,image, description) \
                VALUES (%s,%s,%s, %s,%s, %s)',(category_Id, manufacture_Id, asin, title, image, description))

but I'm getting: 'ascii' codec can't encode character u'\xae' in position 51: ordinal not in range(128)

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
user2985824
  • 65
  • 2
  • 5
  • Looks like one of your inputs is UTF-8 and something somewhere (you didn't paste a stacktrace) is expecting ascii only, and implicit conversion isn't doing the job. – X-Istence Mar 08 '15 at 04:40

2 Answers2

1

One of your parameters is a Unicode string and being implicitly encoded to a byte string using the default ascii codec. Print the type of your parameters to help figure it out:

print type(category_Id)
print type(manufacture_Id)
etc...
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • I'm using Amazon product ApI data. to escape quote and double quote I call the execute method with the text as a second *parametar*. after that I got: "'ascii' codec can't encode character u'\xae' in position 51: ordinal not in range(128)" – user2985824 Mar 08 '15 at 06:06
  • Yes, and one or more (or all) of those parameters are Unicode strings. Because you are mixing byte strings and Unicode strings, Python is implicitly encoding the Unicode strings using the `ascii` codec. Either use all Unicode strings (if mysql supports it) or use all byte strings. Note that your insert command is a byte string. It may be as simple as making that a Unicode string if all your parameters are Unicode, but I don't know if mysql supports that. I don't use it. – Mark Tolonen Mar 08 '15 at 16:38
  • do you mean using this method: .decode('utf-8') I changed my code to : title.decode('utf-8') but it return nothing I mean now title = '' " – user2985824 Mar 09 '15 at 01:55
0
  • The u'\xae in Unicode means ® (Registered Trademark)
  • One of your parameter may be a unicode string containing u'\xae and you must be using Python 2.x
  • You shall try to upgrade your Python to 3.x to prevent this error.

This link may provide more info ---> How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"

Blossom
  • 113
  • 1
  • 8