简体   繁体   中英

How can I encode string in python as the same as javascript escape?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM