我正在为一个单词搜索文本文件。它找到该单词并返回包含该单词的行。这很好,但我想在该行中突出显示或使该单词粗体。如何“突出显示”文本文件中找到的单词?
我可以在Python中执行此操作吗?另外我可以使用更好的方式来获取用户的txt文件路径。特殊字符
def read_file(file):
#reads in the file and print the lines that have searched word.
file_name = raw_input("Enter the .txt file path: ")
with open(file_name) as f:
the_word = raw_input("Enter the word you are searching for: ")
print ""
for line in f:
if the_word in line:
print line
你是什么意思突出行?你的意思是保存一个新文件,并突出显示该行。打印它突出显示?突出显示一个文本编辑器?还有别的吗?我猜你的意思是保存一个新文件,并突出显示该行,但这取决于你想保存的文件格式。 .RTF? HTML吗? .DOC?还有别的吗? – DJMcMayhem
我很抱歉将文件保存为.txt,并突出显示搜索到的单词。 – jeffkrop
.txt用于纯文本,没有highliting。 – DJMcMayhem