项目里面有个需求:给搜索框下面的自动补全列表匹配内容高亮显示
es高亮补全功能是使用的elastic search提供的completion suggester,但是该接口没有对结果匹配部分进行高亮显示的功能,所以在不改变前述实现的情况下,写一个通用函数,对结果列表中字符串进行高亮设置。
def highlight_text(highlight_string, list_res):
for idx in range(len(list_res)):
item = list_res[idx]
item = "\033[0;31m" + highlight_string + "\033[0m" + item.replace(highlight_string, "")
list_res[idx] = item
for item in list_res:
print item
上面第四行,是设置颜色的,可参考下表。
TEXT COLOR | CODE | TEXT STYLE | CODE | BACKGROUND COLOR | CODE |
---|---|---|---|---|---|
Black | 30 | No effect | 0 | Black | 40 |
Red | 31 | Bold | 1 | Red | 41 |
Green | 32 | Underline | 2 | Green | 42 |
Yellow | 33 | Negative1 | 3 | Yellow | 43 |
Blue | 34 | Negative2 | 5 | Blue | 44 |
Purple | 35 | Purple | 45 | ||
Cyan | 36 | Cyan | 46 | ||
White | 37 | White | 47 |
再举几个例子:
print("\033[0;37;40m Normal text\n")
print("\033[2;37;40m Underlined text\033[0;37;40m \n")
print("\033[1;37;40m Bright Colour\033[0;37;40m \n")
print("\033[3;37;40m Negative Colour\033[0;37;40m \n")
print("\033[5;37;40m Negative Colour\033[0;37;40m\n")
print("\033[1;37;40m \033[2;37:40m TextColour BlackBackground TextColour GreyBackground WhiteText ColouredBackground\033[0;37;40m\n")
print("\033[1;30;40m Dark Gray \033[0m 1;30;40m \033[0;30;47m Black \033[0m 0;30;47m \033[0;37;41m Black \033[0m 0;37;41m")
print("\033[1;31;40m Bright Red \033[0m 1;31;40m \033[0;31;47m Red \033[0m 0;31;47m \033[0;37;42m Black \033[0m 0;37;42m")
print("\033[1;32;40m Bright Green \033[0m 1;32;40m \033[0;32;47m Green \033[0m 0;32;47m \033[0;37;43m Black \033[0m 0;37;43m")
print("\033[1;33;40m Yellow \033[0m 1;33;40m \033[0;33;47m Brown \033[0m 0;33;47m \033[0;37;44m Black \033[0m 0;37;44m")
print("\033[1;34;40m Bright Blue \033[0m 1;34;40m \033[0;34;47m Blue \033[0m 0;34;47m \033[0;37;45m Black \033[0m 0;37;45m")
print("\033[1;35;40m Bright Magenta \033[0m 1;35;40m \033[0;35;47m Magenta \033[0m 0;35;47m \033[0;37;46m Black \033[0m 0;37;46m")
print("\033[1;36;40m Bright Cyan \033[0m 1;36;40m \033[0;36;47m Cyan \033[0m 0;36;47m \033[0;37;47m Black \033[0m 0;37;47m")
print("\033[1;37;40m White \033[0m 1;37;40m \033[0;37;40m Light Grey \033[0m 0;37;40m \033[0;37;48m Black \033[0m 0;37;48m")
该处参考:http://ozzmaker.com/add-colour-to-text-in-python/
下面是一种稍复杂一点的写法,可保证高亮词出现在字符串中间位置:
def highlight_text(highlight_string, list_res, color):
for idx in range(len(list_res)):
item = list_res[idx]
match_str = re.finditer(highlight_string, item)
item_highlighted = u""
last_end = 0
for it in match_str:
it = it.regs[0]
item_highlighted += item[last_end:it[0]] + u"<font color='" + color + u"'>" + highlight_string + u"</font>"
last_end = it[1]
item_highlighted += item[last_end:]
list_res[idx] = item_highlighted
for item in list_res:
print item
上面支持一个句子中出现多次需要高亮的关键字,下面是使用python自带的方法实现上述功能的代码:
def highlight_text(highlight_string, list_res, color):
for idx in range(len(list_res)):
item = list_res[idx]
item = re.sub(highlight_string, lambda m: "<font color='" + color + "'>" + m.group(0) + "</font>", item, flags=re.IGNORECASE)
list_res[idx] = item
for item in list_res:
print item