2016-04-14 138 views
0

的代碼是:如何使用python從文件中刪除特定的行?

from datetime import datetime,time 
from csv import reader 

with open('onlyOnce.txt', 'r+') as fonlyOnce: 
    for f_time, sec_time, dte in filter(None, reader(fonlyOnce, delimiter="_")): 

     check_stime=f_time.split(":") 
     Stask_hour=check_stime[0] 
     Stask_minutes=check_stime[1] 
     check_stime = datetime.strptime(f_time,"%H:%m").time() 

     check_etime=sec_time.split(":") 
     Etask_hour=check_etime[0] 
     Etask_minutes=check_etime[1] 

     #check every minute if current information = desired information 
     now = datetime.now() 
     now_time = now.time() 
     date_now = now.date() 

     if (date_now.strftime("%Y-%m-%d") == dte and time(int(Stask_hour),int(Stask_minutes)) <= now_time <= time(int(Etask_hour),int(Etask_minutes))): 
      print("this line in range time: "+ f_time) 
      #delete this line 
      fonlyOnce.write(" ") 
     else: 
      print("Padraic Cunningham") 
fonlyOnce.close() 

此代碼的目標是:

1-對文件中的行循環

2-檢查,如果任何線在它的範圍內當前時間

3-如果是:打印this line in range time: 9:1並從同一文件中刪除此行。

4-文件中的數據是:

7:1_8:35_2016-04-14 
8:1_9:35_2016-04-14 
9:1_10:35_2016-04-14 

5-輸出必須是:

7:1_8:35_2016-04-14 
8:1_9:35_2016-04-14 

因爲最後行有電流time.it的範圍時必須刪除和替換空行。

我的問題是這樣的代碼將清理所有的文件,我不希望出現這種情況:

invaild代碼:fonlyOnce.write(" ")

感謝

+0

你有一個縮進問題,在fonlyOnce的開始處缺少一個空格....已編輯它。 – tfv

+0

我認爲你已經完成了這個工作,試圖在原地進行。你有沒有試過寫出你想保留的臨時文件,然後用臨時文件替換原始文件? – SpoonMeiser

+0

[從Python中的大文件中刪除行的最快方法]的可能重複(http://stackoverflow.com/questions/2329417/fastest-way-to-delete-a-line-from-large-file-in-蟒蛇) – martijnn2008

回答

1

我做了什麼:
1.刪除循環中的確定函數。
2.如果不能滿足您的需求,空單取代數據
3.打開一個新的文件中寫入處理過的數據

def IsFit(f_time, sec_time, dte): 
     check_stime=f_time.split(":") 
     Stask_hour=check_stime[0] 
     Stask_minutes=check_stime[1] 
     check_stime = datetime.strptime(f_time,"%H:%m").time() 

     check_etime=sec_time.split(":") 
     Etask_hour=check_etime[0] 
     Etask_minutes=check_etime[1] 

     #check every minute if current information = desired information 
     now = datetime.now() 
     now_time = now.time() 
     date_now = now.date() 

     if (date_now.strftime("%Y-%m-%d") == dte and time(int(Stask_hour),int(Stask_minutes)) <= now_time <= time(int(Etask_hour),int(Etask_minutes))): 
      return False 
     else: 
      return True 

    with open('onlyOnce.txt', 'r+') as fonlyOnce: 
     res = [ line if IsFit(*line) else [] for line in csv.reader(fonlyOnce, delimiter="_") if line ] 

    with open(NewFile,'wb') as f: 
     wirter = csv.wither(f) 
     wirter.writerows(res) 
+0

好吧,但如果我想總是從「onlyOnce.txt」文件中讀取,更新數據保存在NewFile! – Samah

+0

@Samah是的,它被保存到新文件 – galaxyan

+0

然後!!如果我想覆蓋!我想重寫相同的文件,因爲我只想從它讀取 – Samah

0

蠻力解決方案 - 適用於小尺寸文件

0-創建線路緩衝區

1-環路文件中的線路

1.1-檢查,如果任何線在它的當前時間

1.2-範圍若是:打印該行中的時間範圍:9:1 如果沒有:添加行到緩衝器

2 - 關閉該文件用於讀

3-添加一個空行到緩衝

4-重新打開用於寫入的文件

5-沖洗緩衝到該文件,並保存在F ile

0
  • 你不想編輯您正在閱讀的文件。這是一個壞主意!

  • 相反,你可能要考慮讀取文件 的每一行到一個列表,從列表中刪除不需要的項目,然後在 寫這個列表中的文件。

    但是,如果文件很大,則可能會很慢。

    此外,在您的代碼結束時,您可以撥打fonlyOnce.close(),但您不需要。上下文管理器(with語句)在您離開文件後自動關閉該文件。

相關問題