Tag Archives: AttributeError: ‘str’ object has no attribute ‘decode’

[Solved] Python Error: Attributeerror: ‘STR’ object has no attribute ‘decode’

Solution:
The above problem occurs find: Python37\lib\site-packages\django\db\backends\mysql\operations.py".

Open and modify the inside: query = query.decode(errors='replace') to query = query.encode(errors='replace') and you're done.

vim /usr/local/python3/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

query = query.decode(errors='replace') Change to: query = query.encode(errors='replace')

Python3.x Error:AttributeError: ‘str’ object has no attribute ‘decode’

Python 3. X: attributeerror: ‘STR’ object has no attribute ‘decode’

1. There are some differences between Python 3.X and python 2.X. I encountered two problems as follows:

a. First report: mysqlclient version 1.3 is wrong:

Solution: comment out the line

b. Second, the problem of character set

The error is as follows: file “C:// users/administrator/pcharmprojects/untitled1/venv/lib/site packages/Django/db/backends/MySQL/operations. Py”, line 146, in last_ executed_ query

query = query.decode(errors=’replace’)

AttributeError: ‘str’ object has no attribute ‘decode’

Solution: Comment out here because the character set is not supported

attributeerror: ‘str’ object has no attribute ‘decode’

attributeerror: ‘str’ object has no attribute ‘decode’

The following code in Python 3 will report the above error

print(“Response:”, resp.text.decode(‘unicode_ Solution:
Print (“response:”, resp.text.encode (‘utf-8 ‘). Decode (‘unicode’)_ Escape ‘)
Add. Encode (‘utf-8’) in the middle

Reason for the problem:
in Python 3, a string must be manually specified as a bytecode by encode before it can be decoded