在python中读取用户给定的开始和结束位置之间的文本文件

发布于 2024-12-04 17:36:29 字数 166 浏览 1 评论 0原文

我有一个巨大的文本文件,我想从中选择性地读取几行。 使用tell()我知道我想要读取的位置。

有没有办法可以读取两个位置之间文件中的所有文本? 像 file.read(beginPos, endPos)

或者读取包含 beginPos 的行号和包含 endPos 的行号之间的所有文本?

I have a huge text file from which i want to selectively read a few lines.
Using tell() i know the positions i want to read between.

Is there a way i can read all the text in the file between the two positions?
like file.read(beginPos, endPos)

or maybe, read all text between line number containing beginPos and line number containing endPos?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

溺ぐ爱和你が 2024-12-11 17:36:29

如果您现在有了起点(使用 tell())和终点,您可以简单地执行 file.read(end-start),它会读取 <代码>结束-开始字节。如果开始时的偏移量不正确,请使用 seek() 方法 (file.seek(start))。

If you now the start point (with tell()) and the end point, you could simply do a file.read(end-start), it will read the end-start bytes. If you're not at the correct offset on begining, use the seek() method (file.seek(start)) first.

孤单情人 2024-12-11 17:36:29

您需要打开文件,然后fileobj.seek(beginPos),然后fileobj.read(endPos-beginPos)

You will want to open the file then fileobj.seek(beginPos) and then fileobj.read(endPos-beginPos)

叹倦 2024-12-11 17:36:29

您看过使用内存映射吗? (http://docs.python.org/library/mmap.html)

一旦有了文件的内存映射,您就可以像对字符串(或列表)一样对其进行切片,而无需将整个文件读入内存。

如果您只想读取文件的单个部分一次,则可能会造成不必要的复杂性,但如果您要执行大量 IO,则可以使其更易于管理。

来自Python文档:

import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
    f.write("Hello Python!\n")

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    map = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print map.readline()  # prints "Hello Python!"
    # read content via slice notation
    print map[:5]  # prints "Hello"
    # update content using slice notation;
    # note that new content must have same size
    map[6:] = " world!\n"
    # ... and read again using standard file methods
    map.seek(0)
    print map.readline()  # prints "Hello  world!"
    # close the map
    map.close()

Have you looked at using memory mapping? (http://docs.python.org/library/mmap.html)

Once you have a memory map of the file, you can slice it like you would a string (or list) without having to read the entire file into memory.

It might be unnecessary complexity if you're only going to read a single section of the file once, but it you're going to do a lot of IO, it can make it much easier to manage.

from the python docs:

import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
    f.write("Hello Python!\n")

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    map = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print map.readline()  # prints "Hello Python!"
    # read content via slice notation
    print map[:5]  # prints "Hello"
    # update content using slice notation;
    # note that new content must have same size
    map[6:] = " world!\n"
    # ... and read again using standard file methods
    map.seek(0)
    print map.readline()  # prints "Hello  world!"
    # close the map
    map.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文