简体   繁体   中英

How to convert octal escape sequences with Python

I extract javascript code from PDF, but it is converted octal escape sequences.

I want to convert it to normal JavaScript code.

\040\040\040\040\146\165\156\143\164\151\157\156\040\163\167\050\051\17....

Please advise me.

You can use unicode_escape encoding :

In Python 2.x:

>>> r'\040\040\040\040\146\165\156\143\164\151\157\156'.decode('unicode-escape')
u'    function'

In Python 3.x:

>>> br'\040\040\040\040\146\165\156\143\164\151\157\156'.decode('unicode-escape')
'    function'

This works for both Python 2.x and 3.x:

>>> b'\040\040\040\040\146\165\156\143\164\151\157\156\040\163\167'.decode('utf-8')
'    function sw'

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