How to convert octal escape sequences with Python

前端 未结 2 659
野趣味
野趣味 2020-12-12 02:14

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\         


        
相关标签:
2条回答
  • 2020-12-12 02:39

    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'
    
    0 讨论(0)
  • 2020-12-12 02:55

    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'
    
    0 讨论(0)
提交回复
热议问题