1

I'm having trouble wrapping my brain around this error. It's really basic, but it seems to say the opposite of what is true.

>>> x=b'hi'
>>> urllib.parse.unquote(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 609, in unquote
    if '%' not in string:
TypeError: a bytes-like object is required, not 'str'

I've passed the code a bytes object, I would hope that's bytes-like enough. urllib.parse.unquote() seems to only work with a str object.

So why would it generate an error saying it needs a bytes-like object?

1 Answer 1

1

I suggest to use urllib.parse.unquote_to_bytes, which encodes its string parameter to bytes if it receives a str object.

urllib.parse.unquote_to_bytes(x)
Sign up to request clarification or add additional context in comments.

1 Comment

It's a very useful answer for the question I didn't know I had. Thanks for the tip. I can't mark this the answer as I was asking why the original TypeError is so cryptic.

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.