调试 Python 函数以删除 ANSI 代码 [英] Debugging a Python function to remove ANSI codes

查看:31
本文介绍了调试 Python 函数以删除 ANSI 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题实际上有两个部分.我编写了一个函数,通过简单的替换从字符串中删除 ANSI 代码,但是该函数按原样返回字符串.我尝试过使用 pdb 进行调试,但结果没有意义,因为 Python 本身似乎存在故障.但是我对此表示怀疑,所以我试图弄清楚我在代码中没有看到什么错误.另外,我想知道是否有更好的方法来删除(可能还没有看到)ANSI 代码,而无需每次都修改函数.

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.

这是一个让我感到困惑的 pdb 屏幕截图:pdb 功能调试截图

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

n_text 没有被设置,targets 被设置为一些奇怪的东西,并且执行行指针以某种方式被设置为一个不可执行的行 (157).当我使用 re 而不是 string.replace 时,我也遇到过类似的错误.

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.

功能是

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

'?-人类(苏格拉底)\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?-'

'?- 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?- '

预期收益为

'?- 人类(苏格拉底)\n.\n真的.\n\n?- '

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

把事情放在上下文中,这是一个项目的一部分创建一个命令-line IDE,当查询被传递到 SWI-Prolog 实例(通过 pexpect)并且输出被解析为仅给出实际结果时,就会发生这个特殊问题,这是true. 在这种情况下.

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.

推荐答案

你的代码应该从显示的字符串中清除 ANSI 代码,你确定你调用它正确吗?

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?- '

这篇关于调试 Python 函数以删除 ANSI 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆