【问题标题】:Python3 + How to change the output color when using print command [duplicate]Python3 +使用打印命令时如何更改输出颜色[重复]
【发布时间】:2021-09-13 02:00:33
【问题描述】:

也许你们中的一些人知道并且关于 shell 脚本 - tput 是实用程序使用 terminfo 数据库来使 shell 可以使用终端相关功能的值和信息

在我的 python 脚本中,我有以下示例:(只是长代码中的一个示例)

for TOPIC in list:
  if str(val) in TOPIC:
     word = TOPIC.split()[1]
     print ("Topic " + word + " is successfully configured")
  else:
      try:
       word = TOPIC.split()[1]
       print ("Topic " + word + " bad configuration")
      except IndexError:
       pass

我想将以下印刷的颜色从白色改为绿色

 print ("Topic " + word + " is successfully configured")

或将颜色从白色更改为红色

print ("Topic " + word + " bad configuration")

是否可以像在bash 脚​​本中那样更改python3 中的颜色

【问题讨论】:

标签: python python-3.x linux tput


【解决方案1】:
import os
os.system("")

RED = "\x1B["
GREEN = '\033[32m'

word = "random_topic_name"
print(GREEN+"Topic " + word + "successfully configured" )
print(RED+"Topic " + word + "bad configuration" )

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 2019-11-23
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 2017-07-14
    • 2021-07-14
    相关资源
    最近更新 更多