How to solve the problem with encoding in python?

Applied code and output. Don't know what to do.
name = 'TARC'
response = urllib.request.urlopen('myurl?cl_name=' + name)
data = response.read().decode('utf-8')

Conclusion:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 42-45: ordinal not in range(128)
If I instead of the variable name will substitute the same word only in the format: %d0%ba%d1%80%d0%b0%d0%bc everything will work. Is it possible like that before you insert the variable to decode it to the desired format or is otherwise required to do?
April 3rd 20 at 17:19
1 answer
April 3rd 20 at 17:21
Solution
import urllib.parse

name = urllib.parse.quote('TARC')
response = urllib.request.urlopen('myurl?cl_name=' + name)
data = response.read().decode('utf-8')

So try it. For the future: better, use requests instead of urllib naked
Now it gives an error like:
name = urllib.urlencode('TARC')
AttributeError: module 'urllib' has no attribute 'urlencode' - dejah_Vandervort42 commented on April 3rd 20 at 17:24
@dejah_Vandervort42, I changed the code, try it - moshe commented on April 3rd 20 at 17:27
@moshe, did. With urllib.parse.quote the word(KATP) is converted to %d0%ba%d1%80%d0%b0%d0%bc ? - dejah_Vandervort42 commented on April 3rd 20 at 17:30
@dejah_Vandervort42, Yes - moshe commented on April 3rd 20 at 17:33
@moshe, thanks. - dejah_Vandervort42 commented on April 3rd 20 at 17:36
@dejah_Vandervort42, no, there will be '%D0%BA%D0%B0%D1%82%D0%BF' - moshe commented on April 3rd 20 at 17:39
@moshe, I probably confused with the word "cram". Just use them here and not copied. - dejah_Vandervort42 commented on April 3rd 20 at 17:42

Find more questions by tags Python