Brykyz
Brykyz

Reputation: 657

Change ASCII to characters in string

i'm trying to convert ASCII values in string to normal characters, but whatever i'll try to do, i'll get output like

this\032is\032my\032\output\000

and I need

this is my output

Thanks for your help!

Upvotes: 0

Views: 283

Answers (2)

Brykyz
Brykyz

Reputation: 657

I solved it with

import re
def replace(match):
    return chr(int(match.group(1)))
aux = str(element.text)
regex = re.compile(r"\\(\d{1,3})")
new = regex.sub(replace, aux)

where element.text is string i need to convert

Upvotes: 1

Ryan Haining
Ryan Haining

Reputation: 36802

\032 is octal for the decimal number 26, so it is not in fact a space character, if you use a hex value with the correct number, you should get what you want. 0x20 == 32 below

>>> s = 'this\x20is\x20my\x20\output\00'
>>> print(s)
this is my \output

Upvotes: 0

Related Questions