我正在爲一個單詞搜索文本文件。它找到該單詞並返回包含該單詞的行。這很好,但我想在該行中突出顯示或使該單詞粗體。如何「突出顯示」文本文件中找到的單詞?
我可以在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