I want to add green color to an integer value which is <= 5 and red color to an integer value >5 , inside a dictionary in python.
from colorama import Fore, Back, Style
input_dict = {'key1': 217, 'key2': 2, 'key3': 9, 'key4': 4, 'key5':6}
for key in input_dict:
if input_dict[key] < 5:
<# print input_dict with integer values in red color #>
if output[key]>5:
<# print input_dict with integer values in red color #>
expected output = {'key1': 217 (217 value printed in red color), 'key2': 2 (2 value printed in green color), 'key3': 9 (9 value printed in red color), 'key4': 4 (4 value printed in green color), 'key5': 6 (6 value printed in red color)}
expected output_dictionary = {'key1': 217 (value printed in red color), 'key2': 2 (value printed in green color), 'key3': 9 (value printed in red), 'key4': 4 (value printed in green color), 'key5': 6 (value printed in red color)}
I request your help sincere help here.
The colorama PyPI page is pretty clear on how to access the ANSI escape sequences that need to be printed to change the color of printed text. However, it is quite important to note that:
So, even if you stored the values like
colored_dictwould just look likeAlthough, if you printed those saved values separately(view examples), the colors will show (just remember to reset so that the color doesn't run on to the next print).
If you just wanted to fill in your code snippet, then: (view output)
If you want it to be printed looking like a dictionary, then: