Python:字典值中的八进制转义字符\033在print语句中转换为UTF-8字符

Python:字典值中的八进制转义字符\033在print语句中转换为UTF-8字符,python,dictionary,terminal,escaping,Python,Dictionary,Terminal,Escaping,我已经在terminal和Python 2.7.3中尝试了一些颜色输出。ANSI颜色代码在终端中总是完美无瑕的呈现,除了这一点,我还不能指出比这一特定字典定义更精确的一点 以下是引起混淆的原因: color = { 'white': "\033[1,37m", 'yellow': "\033[1,33m", 'green': "\033[1,32m", 'blue': "\033[1,34m", 'cyan': "\033[

我已经在terminal和Python 2.7.3中尝试了一些颜色输出。ANSI颜色代码在终端中总是完美无瑕的呈现,除了这一点,我还不能指出比这一特定字典定义更精确的一点

以下是引起混淆的原因:

color = {
    'white':    "\033[1,37m",
    'yellow':   "\033[1,33m",
    'green':    "\033[1,32m",
    'blue':     "\033[1,34m",
    'cyan':     "\033[1,36m",
    'red':      "\033[1,31m",
    'magenta':  "\033[1,35m",
    'black':    "\033[1,30m",
    'darkwhite':  "\033[0,37m",
    'darkyellow': "\033[0,33m",
    'darkgreen':  "\033[0,32m",
    'darkblue':   "\033[0,34m",
    'darkcyan':   "\033[0,36m",
    'darkred':    "\033[0,31m",
    'darkmagenta':"\033[0,35m",
    'darkblack':  "\033[0,30m",
    'off':        "\033[0,0m"
}

yellow = "\033[1;33m"
off = "\033[0;0m"

print color['yellow'] + "string to render" + color['off']     # fails to render properly
print "%(yellow)sstring to render%(off)s" % color             # ditto
print "%sstring to render%s" % (color['yellow'], color['off'])# ditto

print yellow + "string to render" + off                       # as intended

pp = pprint.PrettyPrinter(indent=6)
pp.pprint(color)
预打印机的输出:

{ 'black': '\x1b[1,30m',
  'blue': '\x1b[1,34m',
  'cyan': '\x1b[1,36m',
  'darkblack': '\x1b[0,30m',
  'darkblue': '\x1b[0,34m',
  'darkcyan': '\x1b[0,36m',
  'darkgreen': '\x1b[0,32m',
  'darkmagenta': '\x1b[0,35m',
  'darkred': '\x1b[0,31m',
  'darkwhite': '\x1b[0,37m',
  'darkyellow': '\x1b[0,33m',
  'green': '\x1b[1,32m',
  'magenta': '\x1b[1,35m',
  'off': '\x1b[0,0m',
  'red': '\x1b[1,31m',
  'white': '\x1b[1,37m',
  'yellow': '\x1b[1,33m'}
在我看来,这是十六进制格式的正确翻译。尽管如此,字典值并没有正确地传递给print语句。原始或Unicode(出于绝望)字符串文字修饰符都不会改变任何内容。我一定错过了一些很明显的东西。在不支持UTF-8的终端上,Unicode字符被省略

我已经看了好几年的实现了 :

:

还有其他几个。从分析角度看,它们都避免在字典中定义整个序列。我同意这种方法在词汇上是合理的。然而,事实仍然是,字典值中的转义字符没有得到正确解释,例如,在Perl散列、C++的
向量
ized
map
、或C的
struct
(如果类似于map)
char*string

这就引出了一个问题:如果可能的话,根据标准,字典(我们称之为:)插值为什么会偏离纯字符串?是否有特定的原因


以下是固定的色码dict(编辑时制表符缩进,因此似乎会剥离制表符以便阅读):


我浏览了你的源代码,我认为问题在于字典中的颜色定义

如果仔细观察,颜色的字典值类似于白色的\033[1,30m。但是它应该是\033[1;30m。请注意,您使用的是“,(逗号)字符,而不是“;(分号)字符。作为测试,我创建了颜色字典的子集并运行了这些测试

>>> color = {'white' :'\033[1;37m', 'yellow':'\033[1;33m', 'off' : '\033[0;0m'}
>>> print color['white'] + 'string' + color['off']
string   #this string is white in color
>>> print color['yellow'] + 'string' + color['off']
string #this string is yellow in color
>>> color['yellow'] = '\033[1,33m' #incorrect color code - using a , character instead of ;
>>> print color['yellow'] + 'string' + color['off']
string   #prints the string in console default color i.e. not in yellow color
>>> 

希望这对你有所帮助

顺便问一下,你使用的是什么操作系统/终端?linux,问题已经解决了,谢谢你!这里有一个更新的链接,指向截至2021年5月的colorama实现:这可能就是问题所在,使用分号分隔符。哦,伙计,非常感谢你耐心且如此准确地查看我的代码。我广告从另一个perl脚本粘贴了这一部分,并在vim中替换了一个不好的正则表达式。再次感谢您的关注!
CSI = '\033['
def code_to_chars(code):
    return CSI + str(code) + 'm'
class AnsiCodes(object):
    def __init__(self, codes):
        for name in dir(codes):
            if not name.startswith('_'):
                value = getattr(codes, name)
                setattr(self, name, code_to_chars(value))
color = {
    'white':    "\033[1;37m",
    'yellow':   "\033[1;33m",
    'green':    "\033[1;32m",
    'blue':     "\033[1;34m",
    'cyan':     "\033[1;36m",
    'red':      "\033[1;31m",
    'magenta':  "\033[1;35m",
    'black':      "\033[1;30m",
    'darkwhite':  "\033[0;37m",
    'darkyellow': "\033[0;33m",
    'darkgreen':  "\033[0;32m",
    'darkblue':   "\033[0;34m",
    'darkcyan':   "\033[0;36m",
    'darkred':    "\033[0;31m",
    'darkmagenta':"\033[0;35m",
    'darkblack':  "\033[0;30m",
    'off':        "\033[0;0m"
}
>>> color = {'white' :'\033[1;37m', 'yellow':'\033[1;33m', 'off' : '\033[0;0m'}
>>> print color['white'] + 'string' + color['off']
string   #this string is white in color
>>> print color['yellow'] + 'string' + color['off']
string #this string is yellow in color
>>> color['yellow'] = '\033[1,33m' #incorrect color code - using a , character instead of ;
>>> print color['yellow'] + 'string' + color['off']
string   #prints the string in console default color i.e. not in yellow color
>>>