如何使用 python 在具有许多相似行的大文件中获取特定行?

发布于 2024-11-08 01:36:31 字数 839 浏览 5 评论 0原文

在不同的目录中有同名的不同文件。在这些文件中,有些行几乎相等,我只想取出这些行的最后一行(后面还有更多行)并将其写入另一个文件中。

到目前为止我所做的:

#!/usr/bin/env python

import os

def cd_grep():
   for file in os.listdir("."):
     if os.path.isfile(file):
       for line in open("graph.txt"):
                  if " 4.49" in line:                               
                       line_list=[line] 
   g = open('comparation','a') 
   g.write ("%s" % (line[0:4]))
   g.close()
os.chdir('4.294')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.394')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.494')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.594')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.694')
cd_grep()

我创建了一个列表,因为我只会获取整行的特定信息。

最后我发现这个过程仅适用于小文件,并且仅当文件的最后一行包含我正在搜索的术语时。 对于大文件,我收到此消息(在文件内,我希望得到该行):
自愿上下文切换:3403

任何想法或建议将不胜感激。

have different files with same name, in different directories. In these files there are lines which are almost equal, I would like to take out only the last line of these ones( there are more lines after it) and write it in another file.

So far what I have done:

#!/usr/bin/env python

import os

def cd_grep():
   for file in os.listdir("."):
     if os.path.isfile(file):
       for line in open("graph.txt"):
                  if " 4.49" in line:                               
                       line_list=[line] 
   g = open('comparation','a') 
   g.write ("%s" % (line[0:4]))
   g.close()
os.chdir('4.294')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.394')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.494')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.594')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.694')
cd_grep()

I've created a list because I am gonna take only a specific information of the whole line.

Finally I got that this procedure only works for small files and only if the last line of the file contains the term I'm searching.
For big files, I got this message ( inside the file, which I was hoping to get the line):
Voluntary context switches: 3403

Any idea or suggestion will be very appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很糊涂小朋友 2024-11-15 01:36:31

不确定您收到的错误(在上次编辑之后)。

我尝试稍微重写一下代码,希望它能给你一个与你需要的类似的结果(警告:未经测试)。

with open ('comparation', 'a') as write_file:
  for path, dirs, files in os.walk(os.getcwd()):
    for filename in [f for f in files if f == "graph.txt"]:
      filepath = os.path.abspath(os.path.join(path, filename))
      with open(filepath) as f:
        for line in f:
          if " 4.49" in line:
            last = line
        write_file.write("File: %s, Line: %s\n" % (filepath, last[0:4]))        

Not sure about the error you are receiving (after your last edit).

I have tried to rewrite the code a bit, hope it gives you a result similar to what you need (WARNING: not tested).

with open ('comparation', 'a') as write_file:
  for path, dirs, files in os.walk(os.getcwd()):
    for filename in [f for f in files if f == "graph.txt"]:
      filepath = os.path.abspath(os.path.join(path, filename))
      with open(filepath) as f:
        for line in f:
          if " 4.49" in line:
            last = line
        write_file.write("File: %s, Line: %s\n" % (filepath, last[0:4]))        
沫雨熙 2024-11-15 01:36:31

我猜你没有关闭你的文件。

def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           graph_file = open('graph.txt'):
           for line in graph_file:
               if " 4.49" in line:                               
                   line_list=[line] 
           graph_file.close()
    g = open('comparation','a') 
    g.write ("%s" % (line[0:4]))
    g.close()

或者更好地使用 with 打开(并始终关闭)文件。

def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           with open('graph.txt') as graph_file:
               for line in graph_file:
                   if " 4.49" in line:                               
                       line_list=[line] 
    with open('comparation','a') as g:
        g.write ("%s" % (line[0:4]))

I'm guessing you aren't closing your files.

def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           graph_file = open('graph.txt'):
           for line in graph_file:
               if " 4.49" in line:                               
                   line_list=[line] 
           graph_file.close()
    g = open('comparation','a') 
    g.write ("%s" % (line[0:4]))
    g.close()

Or much better use with to open (and always close) your files.

def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           with open('graph.txt') as graph_file:
               for line in graph_file:
                   if " 4.49" in line:                               
                       line_list=[line] 
    with open('comparation','a') as g:
        g.write ("%s" % (line[0:4]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文