Python突出显示终端上json文档中的特定文本

时间:2022-08-29 18:06:42

I am interested in highlighting a specific portion of the json document based on some arbitrary matching algorithm. For example

我有兴趣基于一些任意匹配算法突出显示json文档的特定部分。例如

{
  "text" : "hello world"
}

I searched for "hello" and above json document has hello in it. How to highlight that particular portion "hello" while displaying the document on the terminal using python? Also, the json has to be pretty printed.

我搜索了“你好”,上面的json文件中有你好。如何使用python在终端上显示文档时突出显示特定部分“hello”?此外,json必须打印漂亮。

Expected

预期

{
  "text" :  " `hello` world"
}

Text that is qoutes should be displayed in red color.

qoutes的文本应以红色显示。

1 个解决方案

#1


2  

I can't comment :(

我无法评论:(

The answer here might be able to help you: Print in terminal with colors using Python?

这里的答案可能能够帮助您:使用Python在终端中打印颜色?

For example, you could use termcolor (if you are using a linux style terminal) and replace "hello" with colored("hello", "red")

例如,您可以使用termcolor(如果您使用的是linux样式的终端)并将“hello”替换为彩色(“hello”,“red”)

from termcolor import colored

sample_text = "colored hello there !"
print(sample_text)

print(sample_text.replace("hello", colored("hello", "red")))

#1


2  

I can't comment :(

我无法评论:(

The answer here might be able to help you: Print in terminal with colors using Python?

这里的答案可能能够帮助您:使用Python在终端中打印颜色?

For example, you could use termcolor (if you are using a linux style terminal) and replace "hello" with colored("hello", "red")

例如,您可以使用termcolor(如果您使用的是linux样式的终端)并将“hello”替换为彩色(“hello”,“red”)

from termcolor import colored

sample_text = "colored hello there !"
print(sample_text)

print(sample_text.replace("hello", colored("hello", "red")))