python 提供一段文本和关键词列表进行标红处理

发布时间 2024-01-08 14:11:35作者: 乐乐乐乐乐乐樂
def highlight_keywords_html(text, keywords):
    for keyword in keywords:
        text = text.replace(keyword, '<span style="color:red;">' + keyword + '</span>')
    return text

# 提供一段文本和关键词列表
text = "Python is a high-level programming language. It's widely used for web development, data analysis and machine learning."
keywords = ["Python", "web development"]

# 对关键词进行标红处理
highlighted_text_html = highlight_keywords_html(text, keywords)

print(highlighted_text_html)