开发者

python readlines stop then continue in the nextline

开发者 https://www.devze.com 2023-02-13 01:27 出处:网络
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.

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 r开发者_运维知识库eading 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)


As commenters said, it's not clear why you're exiting the loop, but take a look at the enumerate built-in. For example:

for line_num, line in enumerate(f.readlines()):
  print line_num, line


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...)

fyle = open("input.txt")

lyne_iterator = iter(fyle)
should_do = False
for lyne in lyne_iterator :
  if should_do_something_with(lyne) :
    should_do = True
    break
if should_do :
  do_something(lyne)

# This will continue reading the file where the last loop left off.
for lyne in lyne_iterator :
  do_something_else(lyne)

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.


This can be done using yield

say you have a file sample.txt as follows, and you care about lines starting with keyword:

not what you're looking for
keyword huzzah
balh balh
blah blah
other text
other lines
keyword found it
keyword hey another one
not me
forget it
keyword yes
nope

The following code will do what you want:

def line_search():
    file =open('sample.txt')
    for line in file:
        if line.startswith('keyword'):
            yield line

all_lines = []
for line in line_search():
    all_lines.append(line)

print all_lines

This yields:

['keyword huzzah\n', 'keyword found it\n', 'keyword hey another one\n', 'keyword yes\n']
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号