If you are using python urllib.parse.urlencode() to encode a string, you may get this type error.
TypeError: not a valid non-string sequence or mapping object
In this tutorial, we will introduce you how to fix this problem.
Here is an example:
import urllib.request n = urllib.parse.urlencode('dive').encode('utf-8')+".png" print(n)
Run this code, you will get this error:
Why does this type error occur?
Becuase urllib.parse.urlencode() will encode a dict or sequence of two-element tuples into a URL query string. It means you can use this function to encode a python dict or tuples. However, you can not use it to encode a python string.
How to fix this error?
We can use urllib.parse.quote()
import urllib.request n = urllib.parse.quote('div good', encoding='utf-8')+".png" print(n)
If you want to know how to urlencode or urldecode a python string, you can refer:
A Simple Guide to Python urlencode and urldecode for Beginners – Python Tutorial