python readlines 停止然后在下一行继续
好吧,我有问题。我需要读取行数非常多的文件的行。
当我找到结果时,我停止并退出循环,然后调用另一个函数。 我怎样才能保存我的“行号”,这样当我回来时,我只是继续从这一行阅读,并且我不会再次阅读上面的所有行。
好吧,你是对的,我的问题不清楚。
我有一个有 2 个循环的脚本。
第一个循环逐行读取“file1”,如果找到我要查找的数字,则我调用另一个包含第二个循环的函数。
我正在阅读这两个文件:
for line in open(file_name):
#do the stuff
我想知道“line”的值以及如何使用行值恢复循环
文件非常大,超过 50k 行。
文件1格式:
16000 hello A
17000 hello X
18000 hello Z
22000 hello X
25000 hello Y
文件2有他的格式:
名称interval_startinterval_end
我的目标是读取第二个文件并检查在第一个循环中找到的数字是否在任何间隔中。当我发现它时执行一个动作。
两个文件的编号均按新月顺序排列。我的问题是,对于我在 file1 中找到的每个键号,我都会读取整个 file2。我的观点是继续阅读我在 file2 中停止的位置,因为由于该文件是新月形的,所以我已经读取的所有值都小于我的实际密钥编号,因此我不需要再次读取它们。
eg: my key numbers are 16000, 22000 and 25000
eg: of loop in file2
hello 15000 20000 #first stop, return a value
hello 20001 20050 #first resume
hello 20051 20200
hello 20201 23000 #second stop, return a value
hello 23001 24000 #resume loop (25000 won't be found i know but that's not the problem)
Ok i've a problem. I need to read the lines of a file with a very large number of lines.
When i find a result i stop and exit the loop then call another function.
How can i save my "line number" so when i come back i just resume reading from this line, and i don't read again all the lines above.
Ok you're right i was not clear in my question.
I've a script with 2 loops.
First loop reads "file1" line by line and if the number i'm looking for is found then i call another function witch contains the second loop.
Im reading both files with:
for line in open(file_name):
#do the stuff
I want to know the value of "line" and how to resume the loop with the line value
Files are very big more than 50k lines.
file 1 format:
16000 hello A
17000 hello X
18000 hello Z
22000 hello X
25000 hello Y
File2 has his format:
name interval_start interval_end
My goal is to read this second file and check if the number found in first loop it's in any of the intervals. And when i find it excute an action.
Both files have the numbers in crescent order. My problem is that for each key number i find in file1 i read the whole file2. My point is just to keep reading where i stoped in file2 because as the file is crescent all the values i've already read are minor to my actual key number so i don't need to read them again.
eg: my key numbers are 16000, 22000 and 25000
eg: of loop in file2
hello 15000 20000 #first stop, return a value
hello 20001 20050 #first resume
hello 20051 20200
hello 20201 23000 #second stop, return a value
hello 23001 24000 #resume loop (25000 won't be found i know but that's not the problem)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如评论者所说,目前尚不清楚为什么要退出循环,但请查看 枚举内置。例如:
As commenters said, it's not clear why you're exiting the loop, but take a look at the enumerate built-in. For example:
最简单的方法是在所有循环中使用相同的迭代器。然后,当您到达第二个循环时,您将从另一个循环结束后的行开始。 (下面是未经测试的代码...)
尽管我同意其他人的观点,但您应该尝试将函数调用放入循环中,而不是中断。它更干净、更简单、更容易理解。
The simplest way is to use the same iterator in all the loops. Then when you get to the second loop, you will start at the line just after the other loop ended at. (Untested code follows...)
Although I agree with everyone else that you should try to put your function call in the loop, rather than breaking. It's cleaner, simpler, and easier to understand.
这可以使用
yield
来完成,假设您有一个文件
sample.txt
,如下所示,并且您关心以keyword
开头的行:以下代码会做你想做的事:
这会产生:
This can be done using
yield
say you have a file
sample.txt
as follows, and you care about lines starting withkeyword
:The following code will do what you want:
This yields: