python readlines 停止然后在下一行继续

发布于 2024-10-19 15:00:34 字数 1062 浏览 2 评论 0原文

好吧,我有问题。我需要读取行数非常多的文件的行。

当我找到结果时,我停止并退出循环,然后调用另一个函数。 我怎样才能保存我的“行号”,这样当我回来时,我只是继续从这一行阅读,并且我不会再次阅读上面的所有行。

好吧,你是对的,我的问题不清楚。

我有一个有 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 技术交流群。

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

发布评论

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

评论(3

一紙繁鸢 2024-10-26 15:00:34

正如评论者所说,目前尚不清楚为什么要退出循环,但请查看 枚举内置。例如:

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

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
脱离于你 2024-10-26 15:00:34

最简单的方法是在所有循环中使用相同的迭代器。然后,当您到达第二个循环时,您将从另一个循环结束后的行开始。 (下面是未经测试的代码...)

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)

尽管我同意其他人的观点,但您应该尝试将函数调用放入循环中,而不是中断。它更干净、更简单、更容易理解。

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.

安静 2024-10-26 15:00:34

这可以使用 yield 来完成,

假设您有一个文件 sample.txt,如下所示,并且您关心以 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

以下代码会做你想做的事:

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

这会产生:

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

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']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文