[英]Search file for a text in python
我想在文件中查找文本。我寫了下面的LOC
with open(filename, 'r') as fileread:
for line in fileread:
if 'MyText' in line:
print line
我的輸入文件如下:
000001 Mytext Some text1
000002 asdsadsadsa
000003 safasfsaf
000004 Mytext Some text2
輸出為:
000001 Mytext Some text1
000004 Mytext Some text2
當天晚些時候,當我再次運行相同的代碼時(輸入文件中添加了一些數據)
我的輸入文件如下(添加了新數據):
000001 Mytext Some text1
000002 asdsadsadsa
000003 safasfsaf
000004 Mytext Some text2
.
.
.
.
.
.
010001 Mytext Some text3
我的輸出是:
000001 Mytext Some text1
000004 Mytext Some text2
010001 Mytext Some text3
有沒有一種方法<我可以修改代碼,以便當我第二次運行該程序時,它忽略之前已經考慮過的值,而將以下內容視為唯一輸出
010001 Mytext Some text3
只需讓您的程序將文件最近處理過的行號寫入文件即可。 然后在開始時跳到該行。
一種解決方案是使用編寫器將數據記錄在第二個文本文件中。 這樣,您可以在兩個執行之間進行通信。
在腳本啟動后:
這也考慮了在現有數據中間插入的新數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.