-1

I am using Python 3.x and have a string that contains utf-8 characters, like this:

link='https%3A%2F%2Fwww.google.com'

I would now like to convert the string to ascii, so that it reads 'https://www.google.com'. How can I achieve this? I have tried

link.decode('utf-8')

which throws the exception:

AttributeError: 'str' object has no attribute 'decode'

Is there not an easy way to simply decode a string containing utf-8 characters to plain ascii characters?

Tartan Leaves
  • 13
  • 1
  • 2

1 Answers1

0

You should use urllib.parse.unquote, it will automatically handle the decoding for you https://docs.python.org/3/library/urllib.parse.html#urllib.parse.unquote :

$ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
>>> from urllib.parse import unquote
>>> url = unquote('https%3A%2F%2Fwww.google.com')
>>> url
'https://www.google.com'
d g
  • 1,594
  • 13
  • 13