[英]How do I print the value of a dictionary in color with Python?
我知道如何以彩色打印語句: print("\033[32m Hello! \033[0m")
或 Rich print("[green]Hello![/green]")
。
但是,當字典的值來自列表時,如何以彩色打印? 還是字典的一般值?
前任:
dict = {
"greetings" : ["hello", "bonjour"],
"goodbyes" : ["adios", "4"]
}
newList = ["hola", "barev"]
dict.update({"greetings" : newList})
print(dict["greetings"][0])
print(dict)
上述兩個打印語句將打印為黑色,我將如何使其打印為綠色? 我需要做什么? 如果需要,我需要下載哪些包/庫?
from termcolor import colored
print(colored(dict, 'green'))
有錢就可以
d = {
"greetings" : ["hello", "bonjour"],
"goodbyes" : ["adios", "4"]
}
console.print(d, style="green", highlight=False)
console.print(d["greetings"][0], style="green", highlight=False)
請注意,應避免將 Python 保留字如“dict”和“sum”用作變量。 您可以嘗試使用 f 字符串進行打印:
dic = {
"greetings": ["hello", "bonjour"],
"goodbyes": ["adios", "4"]
}
newList = ["hola", "barev"]
dic.update({"greetings": newList})
hola = dic["greetings"][0]
print(f"\033[32m{hola}\033[0m")
print(f"\033[32m{dic}\033[0m")
或這個:
print("\033[32m{}\033[0m".format(hola))
print("\033[32m{}\033[0m".format(dic))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.