Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert octal escape sequences with Python [duplicate]

Tags:

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.

like image 209
hucuhy Avatar asked May 08 '26 06:05

hucuhy


2 Answers

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'
like image 124
falsetru Avatar answered May 10 '26 18:05

falsetru


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'
like image 24
user3286261 Avatar answered May 10 '26 19:05

user3286261



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!