简体   繁体   English

Python 3-通过行号在大型文本文件中查找行

[英]Python 3 - Looking up line in large text file by line number

Title pretty much says it all: I've spent over a day figuring out how to fetch a single line of text out of a large text file through inputting its line number, without success. 标题几乎说明了一切:我花了整整一天的时间来弄清楚如何通过输入行号从大型文本文件中提取一行文本,但是没有成功。 So far, it seems like the only solutions people are talking about online load the entire text file into a list, but it's simply too large to do so. 到目前为止,似乎人们正在讨论的唯一解决方案是将整个文本文件在线加载到列表中,但是这样做太大了。 The structure of the .txt file I'm working with is literally just a big listing of urls, one per line. 我正在使用的.txt文件的结构实际上只是一大堆url,每行一个。

I've tried using handle.readline() , but that didn't help the pinpointing of the specific line. 我试过使用handle.readline() ,但这并没有帮助您确定特定的行。 Because the file is so large, I can't really justify loading all of its lines into memory using the handle.readlines() method, so that's a bust as well. 由于文件太大,因此我无法真正使用handle.readlines()方法将其所有行都加载到内存中,因此也很handle.readlines() I tried to write a function using a for index,line in enumerate(handle) as found online, but that weirdly returns None . 我尝试使用在线发现的for index,line in enumerate(handle)for index,line in enumerate(handle)编写函数,但是奇怪地返回None Any help is appreciated. 任何帮助表示赞赏。

EDIT: Some code below that does not work: 编辑:下面的某些代码不起作用:

fh = open("file.txt","a+")

def offsetfunc(handle,lineNum):
    line_offset = []
    offset = 0
    for line in handle:
        line_offset.append(offset)
        offset += len(line)
    handle.seek(line_offset[lineNum-1],0)

offsetfunc(fh,1)        #Returns IndexError
print(fh.readline())    #Presumably would be viable, if the previous statement worked?

Also, using the linecache technique loads the file into memory, so yeah, that's non-viable, too. 此外,使用linecache技术加载文件到内存中,这样啊,这是不能存活了。

This program might do what you want: 该程序可能会执行您想要的操作:

def fetch_one_line(filename, linenumber):
    with open(filename) as handle:
        for n, line in enumerate(handle, 1):
            if n == linenumber:
                return line
    print("OOPS! There aren't enough lines in the file")
    return None

my_line = fetch_one_line('input.txt', 5)
print(repr(my_line))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM