0

Lets say that i have function load_from_xml() which loads string from XML file. String contains escape sequences for example: "Here\x20are\x20spaces". When i want to print it I get:

s = str(load_from_xml())
print(s)
>"Here\x20are\x20spaces"

which is not desired output. Desired output would be:

>"Here are spaces"

Any idea why print() ignores escape sequences?

Edit: Sample of function of load_from_xml():

import xml.etree.ElementTree as ET

def load_from_xml():

    xml_string = "<data>Here\\x20are\\x20spaces</data>"  # double \\ so data are stored like \x20 
    root = ET.fromstring(xml_string)
    return root.text
Fredyman
  • 1
  • 2
  • Could you add an example for your 'load_from_xml()' function? I ran the program using your example string and I had no problems. – Ismail Hafeez Mar 07 '21 at 16:16
  • Just found out simmilar question and answer is [here](https://stackoverflow.com/questions/4020539/process-escape-sequences-in-a-string-in-python/4020824#4020824) – Fredyman Mar 07 '21 at 16:59

2 Answers2

0

It's unquote you're looking for :)

import urllib
urllib.parse.unquote("Here\x20are\x20spaces")

EDIT :

Then, simply :

import urllib
urllib.parse.unquote(load_from_xml().replace(r'\\', r'\'))

EDIT :

But no, actually, the simple first solution I gave you will work, looking at your function, you already have simple slashes in your example...

And to answer to your final question : Because it's not a usual escape sequence, it's an url quoting sequence...

Icarwiz
  • 176
  • 6
  • This might work but string that I am getting as input should be written more like "Here\\x20are\\x20spaces" so that the escape sequence is written as string not like unicode char. – Fredyman Mar 07 '21 at 16:50
0
a="Here\x20are\x20spaces"
"".join(c for c in a if  c.isprintable())

Output:

'Here are spaces'

str.isprintable

Return True if all characters in the string are printable or the string is empty, False otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable.

Ajay
  • 4,831
  • 2
  • 21
  • 28
  • This might work but I am looking for something that can convert all escape sequences not just spaces. – Fredyman Mar 07 '21 at 16:46