1

I have a string like this:

downloadFile":"/myportal/ABC/35/audio/182/audio?Id=996\u0026stepNo=0\u0026resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav

I need to change \u0026 (which is hex) to & I even tried this -->

.replace("\u0026","&")  

it didnt work

Ghost
  • 549
  • 8
  • 29
  • Please show your attempt at this. Using that same code I get the correct result. – John Dec 28 '18 at 15:51
  • I guess the original input is something like `a = "\\u0026"`, with 2 backslashes to input a real "\", and OP is asking for a reverse function of `re.escape`. However I can't find a good solution. – Geno Chen Dec 28 '18 at 16:06
  • If my guess is right, then all the answer at this time are wrong, since if we enter `a = "\u0026"` then `a` is now "&", a normal escape, no need to convert. – Geno Chen Dec 28 '18 at 16:19

6 Answers6

2

As my comment said, I guess the "\u0026" is an escaped string.

That is, the real input should be something like

a = "\\u0026"

with double backslashes to enter a real "\".

Then, we may use json.loads as a tricky reverse function for re.escape, for example:

import json
json.loads("{\"downloadFile\":\"/myportal/ABC/35/audio/182/audio?Id=996\\u0026stepNo=0\\u0026resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav\"}")
# output: 
# {'downloadFile': '/myportal/ABC/35/audio/182/audio?Id=996&stepNo=0&resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav'}

Or wrap it into a function:

def deescape(escaped):
    return str(json.loads("{\"s\":\"" + escaped + "\"}"))[7 : -2]

deescape("\\u0026") # return '&'

Update: This solution is not suitable if escaped contains ":". The real solution should be:

# Python 2
def deescape(escaped)
    return escaped.decode('string_escape')

# Python 3
def deescape(escaped)
    return escaped.encode().decode('unicode_escape')
Geno Chen
  • 4,916
  • 6
  • 21
  • 39
1

When it comes to URLs, make you a favor and use urllib, here you need urllib.parse.unquote

import urllib.parse

url = "/myportal/ABC/35/audio/182/audio?Id=996\u0026stepNo=0\u0026resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav"
print(urllib.parse.unquote(url))

If it's Python2.x, use import urllib and urllib.unquote().

Arount
  • 9,853
  • 1
  • 30
  • 43
1

From what I can see there appear to be quotation marks on the colon sign after downloadFile which could be causing an error. You could escape them using a backslash (\) like so:

yourVar = "downloadFile\":\"/myportal/ABC/35/audio/182/audio?Id=996\u0026stepNo=0\u0026resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav"
yourVar.replace("\u0026","&")

Optionally you could define the variable with single quotes, as this could allow your current string to work since the double quote around the colon would be ignored:

yourVar = 'downloadFile":"/myportal/ABC/35/audio/182/audio?Id=996\u0026stepNo=0\u0026resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav'
yourVar.replace("\u0026","&")

For reference I have added what I ran in my console since the code worked fine for me

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
aaaakshat
  • 809
  • 10
  • 19
0

Try this code:

downloadFile ="/myportal/ABC/35/audio/182/audio?Id=996\u0026stepNo=0\u0026resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav"

print(downloadFile.replace("\u0026","&"))

Output :

/myportal/ABC/35/audio/182/audio?Id=996&stepNo=0&resource=996-0-dde82d48-3097-4835-a1e4-30602c460fd7-1.wav
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Usman
  • 1,983
  • 15
  • 28
0

None worked the only thing that worked for me is by using --> \u0026 note double \

myStr.replace("\\u0026","&")
Ghost
  • 549
  • 8
  • 29
0

I just ran in to this issue and slightly adjusted the code that Geno Chen provided which worked for me. The function I used was:

def deescape(escaped):
return escaped.encode().decode('unicode_escape').encode().decode("utf-8", "replace")

I used it inside of a lambda function to apply this to all records inside a pandas series.