如何使用python从文件中删除特定的行?

问题描述:

的代码是:如何使用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://*.com/questions/2329417/fastest-way-to-delete-a-line-from-large-file-in-蟒蛇) – martijnn2008

我做了什么:
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-创建线路缓冲区

1-环路文件中的线路

1.1-检查,如果任何线在它的当前时间

1.2-范围若是:打印该行中的时间范围:9:1 如果没有:添加行到缓冲器

2 - 关闭该文件用于读

3-添加一个空行到缓冲

4-重新打开用于写入的文件

5-冲洗缓冲到该文件,并保存在F ile

  • 你不想编辑您正在阅读的文件。这是一个坏主意!

  • 相反,你可能要考虑读取文件 的每一行到一个列表,从列表中删除不需要的项目,然后在 写这个列表中的文件。

    但是,如果文件很大,则可能会很慢。

    此外,在您的代码结束时,您可以拨打fonlyOnce.close(),但您不需要。上下文管理器(with语句)在您离开文件后自动关闭该文件。