阅读文件python中的上一行 [英] Read previous line in a file python

查看:671
本文介绍了阅读文件python中的上一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在遍历文件时,我需要获取文件中前一行的值并将其与当前行进行比较.该文件很大,因此我无法完整读取它,也无法使用linecache随机访问行号,因为无论如何库函数仍会将整个文件读入内存.

I need to get the value of the previous line in a file and compare it with the current line as I'm iterating through the file. The file is HUGE so I can't read it whole or randomly accessing a line number with linecache because the library function still reads the whole file into memory anyway.

编辑很抱歉,我忘记了我必须向后读取文件的提示.

EDIT I'm so sorry I forgot the mention that I have to read the file backwardly.

EDIT2

我尝试了以下操作:

 f = open("filename", "r")
 for line in reversed(f.readlines()): # this doesn't work because there are too many lines to read into memory

 line = linecache.getline("filename", num_line) # this also doesn't work due to the same problem above. 

推荐答案

在迭代到下一个时只需保存上一个

Just save the previous when you iterate to the next

prevLine = ""
for line in file:
    # do some work here
    prevLine = line

这将在循环时将前一行存储在prevLine

This will store the previous line in prevLine while you are looping

编辑显然是OP需要向后读取此文件:

edit apparently OP needs to read this file backwards:

aa,经过一个小时的研究,我在内存有限的情况下多次失败.

aaand after like an hour of research I failed multiple times to do it within memory constraints

在这里你去林,那个家伙知道他在做什么,这是他最好的主意:

Here you go Lim, that guy knows what he's doing, here is his best Idea:

常规方法2:读取整个文件,存储行的位置

General approach #2: Read the entire file, store position of lines

使用这种方法,您还可以一次读取整个文件,但是 而不是将整个文件(所有文本)存储在内存中, 将二进制位置存储在文件中每行开始的位置. 您可以将这些职位存储在与该职位相似的数据结构中 在第一种方法中存储行.

With this approach, you also read through the entire file once, but instead of storing the entire file (all the text) in memory, you only store the binary positions inside the file where each line started. You can store these positions in a similar data structure as the one storing the lines in the first approach.

无论您想读取第X行,您都必须从 文件,从您存储在该行开头的位置开始.

Whever you want to read line X, you have to re-read the line from the file, starting at the position you stored for the start of that line.

优点:几乎与第一种方法一样容易实现缺点:可以采用 一会儿看大文件

Pros: Almost as easy to implement as the first approach Cons: can take a while to read large files

这篇关于阅读文件python中的上一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆