【问题标题】:Colored output in pythonpython中的彩色输出
【发布时间】:2014-10-23 09:19:46
【问题描述】:

我正在使用 python 2.7,我已经在查看:

Print in terminal with colors using Python?

Python Terminal Menu? Terminal Coloring? Terminal progress display?

我仍然遇到同样的错误。运行代码为:

print '\033[1;31m' + '@%s:' + '[1;m' + '\033[1;32m' + '%s - id = %s\n' + '\033[1;m' % (status.text1.encode('utf-8'), status.text2.encode('utf-8'), status.text3.encode('utf-8'))

得到的错误是:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    mostrar()
  File "C:\Documents and Settings\Administrador.DESKTOP\Mis documentos\test.py", line 16, in mostrar
        print '\033[1;31m' + '@%s:' + '[1;m' + '\033[1;32m' + '%s - id = %s\n' + '\033[1;m' % (status.text1.encode('utf-8'), status.text2.encode('utf-8'), status.text3.encode('utf-8'))
TypeError: not all arguments converted during string formatting

如您所见,我在 windows 中使用 python。

问题是如何避免错误?我已经用colorama了,还是一样的错误。

【问题讨论】:

    标签: python windows python-2.7


    【解决方案1】:

    尝试在要连接的格式字符串周围加上括号。 % 绑定的优先级高于+,所以错误来自:

    '\033[1;m' % (... 3 arguments ...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2023-02-25
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多