The Python Oracle

'str' object has no attribute 'decode'. Python 3 error?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Melt

--

Chapters
00:00 'Str' Object Has No Attribute 'Decode'. Python 3 Error?
00:38 Accepted Answer Score 301
00:58 Answer 2 Score 57
01:17 Answer 3 Score 56
01:25 Answer 4 Score 75
01:43 Thank you

--

Full question
https://stackoverflow.com/questions/2858...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #imaplib

#avk47



ACCEPTED ANSWER

Score 301


You are trying to decode an object that is already decoded. You have a str, there is no need to decode from UTF-8 anymore.

Simply drop the .decode('utf-8') part:

header_data = data[1][0][1]



ANSWER 2

Score 75


If you land here using jwt authentication after the PyJWT v2.0.0 release (22/12/2020), you might want to freeze your version of PyJWT to the previous release in your requirements.txt file.

PyJWT==1.7.1



ANSWER 3

Score 57


Begining with Python 3, all strings are unicode objects.

  a = 'Happy New Year' # Python 3
  b = unicode('Happy New Year') # Python 2

The instructions above are the same. So I think you should remove the .decode('utf-8') part because you already have a unicode object.




ANSWER 4

Score 56


Use it by this Method:

str.encode().decode()