0
我最近在Python 3.5中編寫了這個腳本來爲一個給定的字符串搜索一個文本文件,我似乎無法弄清楚如何讓腳本在單詞「log」之後移除其餘的單詞,顯示在行中。Python文件搜索腳本
file1 = input ('What is the name of the file? ')
search_string = input ('What are you looking for? ')
with open(file1) as cooldude:
for line in cooldude:
line = line.rstrip()
if search_string in line:
print(line)
一個例子是: 「我想保持這種東西登錄我不想要這個東西。」 我想刪除包括單詞「日誌」後的所有內容。謝謝!
如果一個行包含單詞「** **登錄IC」或「** **日誌arithm 「?或「ana ** log **」? –
我要搜索的文件不會有任何該行中的單詞。 – krisP
so'line,sep,_ = line.partition('log')'或'line = line.split('log')[0]' –