gpt4 book ai didi

python - 读取文件python中的前一行

转载 作者:太空狗 更新时间:2023-10-29 20:37:16 25 4
gpt4 key购买 nike

我需要获取文件中上一行的值,并在遍历文件时将其与当前行进行比较。该文件很大,所以我无法读取整个文件或使用 linecache 随机访问行号,因为库函数仍然会将整个文件读入内存。

编辑 很抱歉,我忘了提到我必须向后阅读文件。

编辑2

我尝试了以下方法:

 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.

最佳答案

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

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

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

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

aa 经过大约一个小时的研究,我在内存限制下多次失败

Here Lim,你去吧,那家伙知道他在做什么,这是他最好的主意:

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.

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 - 读取文件python中的前一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17373118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com