Convert str with percents (url) to usual str [duplicate] - python

This question already has answers here:
Decode escaped characters in URL
(5 answers)
Closed 8 years ago.
I have strings like C%2B%2B_name.zip which are supposed as url encoded. How to convert them to C++_name.zip?
Py 3.x.

For Python 3, you will need to use:
urllib.parse.unquote('C%2B%2B_name.zip')
See urllib.parse.unquote.

All you need is URL library
import urllib
print urllib.unquote('C%2B%2B_name.zip')
and if you have names with other characters (not only English), then you can add .decode('utf8')

Related

Hex to plain ASCII? Python 2.7.13 [duplicate]

This question already has answers here:
Decode escaped characters in URL
(5 answers)
Closed 2 years ago.
Im trying to turn this:
%73%6c%61%70%72%69%73%65%40%6c%69%65%6e%6d%75%6c%74%69%6d%65%64%69%61%2e%63%6f%6d
into this:
slaprise#lienmultimedia.com
and my brain is exploding..
Any help would be appreciated.
Thank you
Python 2.7.17 (should work for Python 2.7.13)
import urllib2
url = urllib2.unquote("%73%6c%61%70%72%69%73%65%40%6c%69%65%6e%6d%75%6c%74%69%6d%65%64%69%61%2e%63%6f%6d")
print(url)
# slaprise#lienmultimedia.com
You are trying to URL-decode that string, use urllib:
from urllib import unquote
url = unquote("%73%6c%61%70%72%69%73%65%40%6c%69%65%6e%6d%75%6c%74%69%6d%65%64%69%61%2e%63%6f%6d")
# url = slaprise#lienmultimedia.com

Changing string to ascii in python [duplicate]

This question already has answers here:
Convert a Unicode string to a string in Python (containing extra symbols)
(12 answers)
Closed 3 years ago.
I need to convert word
name = 'Łódź'
to ASCII characters
output: 'Lodz'
I can't import any library like unicodedata.
I need to do it in clear python.
I've tried to encode than decode and nothing worked.
Well, a simple method would be to map and replace. This also does not require any special imports.
name = 'Łódź'
name=name.replace('Ł','L')
name=name.replace('ó','o')
name=name.replace('ź','z')
print(name)

Python print ascii character in string instead of value [duplicate]

This question already has answers here:
Decode HTML entities in Python string?
(6 answers)
Closed 3 years ago.
I have a string "hello[ World]" and I want to convert it to "hello[World]"
I tried something like this:
a.encode("utf-8").decode("ascii")
I got back same string as input.
Try this:
import html
html.unescape("LASIX [FUROSEMIDE]")
This produces:
'LASIX [FUROSEMIDE]'

Parsing a URL using regular expression [duplicate]

This question already has answers here:
What's the u prefix in a Python string?
(5 answers)
Closed 6 years ago.
I am trying to parse the 'Meghan' part from the line:
link = http://python-data.dr-chuck.net/known_by_Meghan.html
...with the following regex:
print re.findall('by_(\S+).html$',link)
I am getting the output:
[u'Meghan']
Why I am getting the 'u'?
It means unicode. Depending on what you'll do with it, you can ignore it for the most part, of you can convert it to ascii by doing .encode('ascii')

Conversion of strings like \\uXXXX in python [duplicate]

This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
I receive a string like this from a third-party service:
>>> s
'\\u0e4f\\u032f\\u0361\\u0e4f'
I know that this string actually contains sequences of a single backslash, lowercase u etc. How can I convert the string such that the '\\u0e4f' is replaced by '\u0e4f' (i.e. '๏'), etc.? The result for this example input should be '๏̯͡๏'.
In 2.x:
>>> u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
u'\u0e4f\u032f\u0361\u0e4f'
>>> print u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
๏̯͡๏
There's an interesting list of encodings supported by .encode() and .decode() methods. Those magic ones in the second table include the unicode_escape.
Python3:
bytes("\\u0e4f\\u032f\\u0361\\u0e4f", "ascii").decode("unicode-escape")

Categories