AttributeError: 'str' object has no attribute 'decode' in Python 3
Codemia Knowledge Hub ·
AttributeError: 'str' object has no attribute 'decode' means you called .decode() on a string in Python 3. A Python 3 str is already Unicode text, so there is nothing to decode: .decode() exists only on bytes. Remove the .decode() call, or fix the source so it produces bytes before you decode.
The error usually shows up when porting Python 2 code, or when a library returns str where you expected bytes.
How str and bytes changed from Python 2 to Python 3
Python 3's str took the place of unicode, and bytes took the place of the old str. Since str is already text, calling .decode() on it has no meaning.
Remove .decode() when the value is already str
Check the type when it could be either
If you don't know whether you will get str or bytes:
Where the error typically comes from
Reading files
HTTP responses
Base64 encoding
JSON handling
Database results
encode() versus decode()
Related mistakes
- Leftover
str.decode()calls after a Python 2 migration: either remove the call or make sure the variable holdsbytesinstead ofstr. - Mixing
strandbytesin concatenation:"text" + b"bytes"raisesTypeErrorin Python 3. Decode the bytes or encode the string so both operands have the same type. .encode().decode()as a no-op:text.encode('utf-8').decode('utf-8')goes to bytes and straight back to str, which does nothing for valid UTF-8 text. Remove both calls.- Assuming all bytes are UTF-8:
.decode('utf-8')on bytes that are not valid UTF-8 (for example Latin-1 data or binary files) raisesUnicodeDecodeError. Use.decode('latin-1')or.decode('utf-8', errors='replace')when the encoding is unknown. - Libraries that changed return types: some libraries returned
bytesin older versions andstrin newer ones, so a.decode()on the result breaks after an upgrade. Check the changelog and use anisinstancecheck for compatibility.
Related reading
- Strange SQLAlchemy error message TypeError 'dict' object does not support indexing
- String comparison in Python is vs.
- String Distance Matrix in Python
- String formatting vs. .format vs. f-string literal
- String similarity metrics in Python
- String to Dictionary in Python
- Strip HTML from strings in Python
- Stripping everything but alphanumeric chars from a string in Python
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.