我有兴趣根据一些任意匹配算法突出显示 json 文档的特定部分。例如
{
"text" : "hello world"
}
我搜索了“hello”,上面的 json 文档中有 hello。如何在使用 python 在终端上显示文档时突出显示特定部分“hello”?此外,json 必须打印得很漂亮。
预期的
{
"text" : " `hello` world"
}
qoutes 文本应以红色显示。
我无法评论:(
这里的答案可能会对您有所帮助:使用 Python 在终端中打印颜色?
例如,您可以使用 termcolor(如果您使用的是 linux 风格的终端)并将“hello”替换为 coloured("hello", "red")
from termcolor import colored
sample_text = "colored hello there !"
print(sample_text)
print(sample_text.replace("hello", colored("hello", "red")))