Home > Software engineering >  How can I encode string in python as the same as javascript escape?
How can I encode string in python as the same as javascript escape?

Time:01-05

if I escape '가온' in javascript,

the result would be:

escape('가온')
'%uAC00%uC628'

I want to get the same result like the js in python.

However, if I encode as ascii like:

byte_string= "누리".encode('ascii', 'backslashreplace')
b'\\ub204\\ub9ac'

the result isn't same. How can I?

CodePudding user response:

Something is confusing in your question: first you encode '가온' then '누리'.

I think encoding with 'backslashreplace' can work as long as you decode and replace the backslashes with %:

"가온".encode('ascii', 'backslashreplace').decode().replace('\\', '%')

Output:

%uac00%uc628

CodePudding user response:

JavaScript escape() is URL escape.

You can get this using urllib in Python.

Note that URLs may have multiple different encodings for the same characters. However any valid URL parses should accept any encoding.

  • Related