python colorama 打印所有颜色
python colorama print all colors
初学Python,遇到了colorama。作为一个测试项目,我想打印出 colorama 中所有可用的颜色。
from colorama import Fore
from colorama import init as colorama_init
colorama_init(autoreset=True)
colors = [x for x in dir(Fore) if x[0] != "_"]
for color in colors:
print(color + f"{color}")
当然这会像这样输出全黑输出:
BLACKBLACK
BLUEBLUE
CYANCYAN
...
因为 Dir(Fore) 只是给了我 Fore.BLUE
、Fore.GREEN
、...
的字符串表示形式
有没有办法访问所有前景颜色 属性 以便它们真正起作用,如:
print(Fore.BLUE + "Blue")
或者换句话说,这样可以更好地表达我的问题。
我想写这个:
print(Fore.BLACK + 'BLACK')
print(Fore.BLUE + 'BLUE')
print(Fore.CYAN + 'CYAN')
print(Fore.GREEN + 'GREEN')
print(Fore.LIGHTBLACK_EX + 'LIGHTBLACK_EX')
print(Fore.LIGHTBLUE_EX + 'LIGHTBLUE_EX')
print(Fore.LIGHTCYAN_EX + 'LIGHTCYAN_EX')
print(Fore.LIGHTGREEN_EX + 'LIGHTGREEN_EX')
print(Fore.LIGHTMAGENTA_EX + 'LIGHTMAGENTA_EX')
print(Fore.LIGHTRED_EX + 'LIGHTRED_EX')
print(Fore.LIGHTWHITE_EX + 'LIGHTWHITE_EX')
print(Fore.LIGHTYELLOW_EX + 'LIGHTYELLOW_EX')
print(Fore.MAGENTA + 'MAGENTA')
print(Fore.RED + 'RED')
print(Fore.RESET + 'RESET')
print(Fore.WHITE + 'WHITE')
print(Fore.YELLOW + 'YELLOW')
简而言之:
for color in all_the_colors_that_are_available_in_Fore:
print('the word color in the representing color')
#or something like this?
print(Fore.color + color)
帕特里克对该问题的评论中详细描述了它打印颜色名称两次的原因。
Is their a way to access all the Fore Color property so they actualy work as in
根据:https://pypi.org/project/colorama/
您可以使用其他方式打印彩色字符串,例如 print(Fore.RED + 'some red text')
您可以使用 termcolor
模块中的 colored 函数,该函数接受一个字符串和一种颜色来为该字符串着色。但并非所有 Fore
颜色都受支持,因此您可以执行以下操作:
from colorama import Fore
from colorama import init as colorama_init
from termcolor import colored
colorama_init(autoreset=True)
colors = [x for x in dir(Fore) if x[0] != "_"]
colors = [i for i in colors if i not in ["BLACK", "RESET"] and "LIGHT" not in i]
for color in colors:
print(colored(color, color.lower()))
希望这能回答您的问题。
编辑:
我阅读了有关 Fore
项目的更多信息,我发现您可以检索包含每种颜色作为键并将其代码作为值的字典,因此您可以执行以下操作以将所有颜色包含在 Fore
:
from colorama import Fore
from colorama import init as colorama_init
colorama_init(autoreset=True)
colors = dict(Fore.__dict__.items())
for color in colors.keys():
print(colors[color] + f"{color}")
初学Python,遇到了colorama。作为一个测试项目,我想打印出 colorama 中所有可用的颜色。
from colorama import Fore
from colorama import init as colorama_init
colorama_init(autoreset=True)
colors = [x for x in dir(Fore) if x[0] != "_"]
for color in colors:
print(color + f"{color}")
当然这会像这样输出全黑输出:
BLACKBLACK
BLUEBLUE
CYANCYAN
...
因为 Dir(Fore) 只是给了我 Fore.BLUE
、Fore.GREEN
、...
有没有办法访问所有前景颜色 属性 以便它们真正起作用,如:
print(Fore.BLUE + "Blue")
或者换句话说,这样可以更好地表达我的问题。
我想写这个:
print(Fore.BLACK + 'BLACK')
print(Fore.BLUE + 'BLUE')
print(Fore.CYAN + 'CYAN')
print(Fore.GREEN + 'GREEN')
print(Fore.LIGHTBLACK_EX + 'LIGHTBLACK_EX')
print(Fore.LIGHTBLUE_EX + 'LIGHTBLUE_EX')
print(Fore.LIGHTCYAN_EX + 'LIGHTCYAN_EX')
print(Fore.LIGHTGREEN_EX + 'LIGHTGREEN_EX')
print(Fore.LIGHTMAGENTA_EX + 'LIGHTMAGENTA_EX')
print(Fore.LIGHTRED_EX + 'LIGHTRED_EX')
print(Fore.LIGHTWHITE_EX + 'LIGHTWHITE_EX')
print(Fore.LIGHTYELLOW_EX + 'LIGHTYELLOW_EX')
print(Fore.MAGENTA + 'MAGENTA')
print(Fore.RED + 'RED')
print(Fore.RESET + 'RESET')
print(Fore.WHITE + 'WHITE')
print(Fore.YELLOW + 'YELLOW')
简而言之:
for color in all_the_colors_that_are_available_in_Fore:
print('the word color in the representing color')
#or something like this?
print(Fore.color + color)
帕特里克对该问题的评论中详细描述了它打印颜色名称两次的原因。
Is their a way to access all the Fore Color property so they actualy work as in
根据:https://pypi.org/project/colorama/
您可以使用其他方式打印彩色字符串,例如 print(Fore.RED + 'some red text')
您可以使用 termcolor
模块中的 colored 函数,该函数接受一个字符串和一种颜色来为该字符串着色。但并非所有 Fore
颜色都受支持,因此您可以执行以下操作:
from colorama import Fore
from colorama import init as colorama_init
from termcolor import colored
colorama_init(autoreset=True)
colors = [x for x in dir(Fore) if x[0] != "_"]
colors = [i for i in colors if i not in ["BLACK", "RESET"] and "LIGHT" not in i]
for color in colors:
print(colored(color, color.lower()))
希望这能回答您的问题。
编辑:
我阅读了有关 Fore
项目的更多信息,我发现您可以检索包含每种颜色作为键并将其代码作为值的字典,因此您可以执行以下操作以将所有颜色包含在 Fore
:
from colorama import Fore
from colorama import init as colorama_init
colorama_init(autoreset=True)
colors = dict(Fore.__dict__.items())
for color in colors.keys():
print(colors[color] + f"{color}")