How can I remove the ANSI escape sequences from a string in python

前端 未结 6 731
太阳男子
太阳男子 2020-11-22 14:26

This is my string:

\'ls\\r\\n\\x1b[00m\\x1b[01;31mexamplefile.zip\\x1b[00m\\r\\n\\x1b[01;31m\'

I was using code to retrieve the output from

6条回答
  •  春和景丽
    2020-11-22 14:52

    The suggested regex didn't do the trick for me so I created one of my own. The following is a python regex that I created based on the spec found here

    ansi_regex = r'\x1b(' \
                 r'(\[\??\d+[hl])|' \
                 r'([=<>a-kzNM78])|' \
                 r'([\(\)][a-b0-2])|' \
                 r'(\[\d{0,2}[ma-dgkjqi])|' \
                 r'(\[\d+;\d+[hfy]?)|' \
                 r'(\[;?[hf])|' \
                 r'(#[3-68])|' \
                 r'([01356]n)|' \
                 r'(O[mlnp-z]?)|' \
                 r'(/Z)|' \
                 r'(\d+)|' \
                 r'(\[\?\d;\d0c)|' \
                 r'(\d;\dR))'
    ansi_escape = re.compile(ansi_regex, flags=re.IGNORECASE)
    

    I tested my regex on the following snippet (basically a copy paste from the ascii-table.com page)

    \x1b[20h    Set
    \x1b[?1h    Set
    \x1b[?3h    Set
    \x1b[?4h    Set
    \x1b[?5h    Set
    \x1b[?6h    Set
    \x1b[?7h    Set
    \x1b[?8h    Set
    \x1b[?9h    Set
    \x1b[20l    Set
    \x1b[?1l    Set
    \x1b[?2l    Set
    \x1b[?3l    Set
    \x1b[?4l    Set
    \x1b[?5l    Set
    \x1b[?6l    Set
    \x1b[?7l    Reset
    \x1b[?8l    Reset
    \x1b[?9l    Reset
    \x1b=   Set
    \x1b>   Set
    \x1b(A  Set
    \x1b)A  Set
    \x1b(B  Set
    \x1b)B  Set
    \x1b(0  Set
    \x1b)0  Set
    \x1b(1  Set
    \x1b)1  Set
    \x1b(2  Set
    \x1b)2  Set
    \x1bN   Set
    \x1bO   Set
    \x1b[m  Turn
    \x1b[0m Turn
    \x1b[1m Turn
    \x1b[2m Turn
    \x1b[4m Turn
    \x1b[5m Turn
    \x1b[7m Turn
    \x1b[8m Turn
    \x1b[1;2    Set
    \x1b[1A Move
    \x1b[2B Move
    \x1b[3C Move
    \x1b[4D Move
    \x1b[H  Move
    \x1b[;H Move
    \x1b[4;3H   Move
    \x1b[f  Move
    \x1b[;f Move
    \x1b[1;2    Move
    \x1bD   Move/scroll
    \x1bM   Move/scroll
    \x1bE   Move
    \x1b7   Save
    \x1b8   Restore
    \x1bH   Set
    \x1b[g  Clear
    \x1b[0g Clear
    \x1b[3g Clear
    \x1b#3  Double-height
    \x1b#4  Double-height
    \x1b#5  Single
    \x1b#6  Double
    \x1b[K  Clear
    \x1b[0K Clear
    \x1b[1K Clear
    \x1b[2K Clear
    \x1b[J  Clear
    \x1b[0J Clear
    \x1b[1J Clear
    \x1b[2J Clear
    \x1b5n  Device
    \x1b0n  Response:
    \x1b3n  Response:
    \x1b6n  Get
    \x1b[c  Identify
    \x1b[0c Identify
    \x1b[?1;20c Response:
    \x1bc   Reset
    \x1b#8  Screen
    \x1b[2;1y   Confidence
    \x1b[2;2y   Confidence
    \x1b[2;9y   Repeat
    \x1b[2;10y  Repeat
    \x1b[0q Turn
    \x1b[1q Turn
    \x1b[2q Turn
    \x1b[3q Turn
    \x1b[4q Turn
    \x1b<   Enter/exit
    \x1b=   Enter
    \x1b>   Exit
    \x1bF   Use
    \x1bG   Use
    \x1bA   Move
    \x1bB   Move
    \x1bC   Move
    \x1bD   Move
    \x1bH   Move
    \x1b12  Move
    \x1bI  
    \x1bK  
    \x1bJ  
    \x1bZ  
    \x1b/Z 
    \x1bOP 
    \x1bOQ 
    \x1bOR 
    \x1bOS 
    \x1bA  
    \x1bB  
    \x1bC  
    \x1bD  
    \x1bOp 
    \x1bOq 
    \x1bOr 
    \x1bOs 
    \x1bOt 
    \x1bOu 
    \x1bOv 
    \x1bOw 
    \x1bOx 
    \x1bOy 
    \x1bOm 
    \x1bOl 
    \x1bOn 
    \x1bOM 
    \x1b[i 
    \x1b[1i
    \x1b[4i
    \x1b[5i
    

    Hopefully this will help others :)

提交回复
热议问题