简体   繁体   中英

Debugging a Python function to remove ANSI codes

There are actually two parts to my question. I've written a function to remove ANSI codes from a string by simple replacement, however the function returns the string as is. I've tried debugging with pdb, but the results aren't making sense, as in it seems as though there's a glitch in Python itself. However I doubt that, so I'm trying to figure out what error I'm not seeing in the code. Also I would like to know if there's a better way to remove (potentially not yet seen) ANSI code without modifying the function each time.

Here's a shot of the pdb screen that has me puzzled: pdb screenshot of function debugging

n_text doesn't get set, targets is set to something weird and the execution line pointer somehow gets set to a non-executable line (157). I have been having similar errors when I use re instead of string.replace .

The function is

def clean_ansi(text, remove=''):
    # remove ANSI control codes
    n_text = text
    targets = [''.join([chr(cde) for cde in [27, 91, 67]]),
               ''.join([chr(cde) for cde in [27, 91, 48, 109]]),
               ''.join([chr(cde) for cde in [27, 91, 49, 109]])]

    for target in targets:
        n_text = n_text.replace(target, '')
    return n_text

For this example the string that I'm trying to clean is

'?- human(socrates)\\n\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C\\x1b[C.\\n\\x1b[1mtrue.\\x1b[0m\\n\\n?- '

and the expected return is

'?- human(socrates)\\n.\\ntrue.\\n\\n?- '

Putting things in context, this is part of a project to create a command-line IDE , and this particular issue occurs when a query is passed to a SWI-Prolog instance (via pexpect) and the output is parsed to give only the actual result, which is true. in this case.

Your code should clean up the ANSI codes from the string presented, are you sure you're calling it right?

Either way, it will strip only the selected codes and is not a particularly elegant or performant way to do it - I'd suggest you to use regex and save yourself some trouble:

import re

ANSI_CLEANER = re.compile(r"(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]")

clean_string = ANSI_CLEANER.sub("", your_string)

print(repr(clean_string))
# prints '?- human(socrates)\n.\ntrue.\n\n?- '

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