[英]Read a text file between user given starting and ending position in python
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. 使用tell()我知道我想阅读的位置。
Is there a way i can read all the text in the file between the two positions? 有没有办法我可以读取两个位置之间文件中的所有文本? like file.read(beginPos, endPos) 像file.read(beginPos,endPos)
or maybe, read all text between line number containing beginPos and line number containing endPos? 还是读取包含beginPos的行号和包含endPos的行号之间的所有文本?
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. 如果现在是起点(带有tell()
)和终点,则可以简单地执行file.read(end-start)
,它将读取end-start
字节。 If you're not at the correct offset on begining, use the seek() method ( file.seek(start)
) first. 如果开始时的偏移量不正确,请首先使用seek()方法( file.seek(start)
)。
您将要打开文件,然后打开fileobj.seek(beginPos)
,然后fileobj.read(endPos-beginPos)
Have you looked at using memory mapping? 您是否看过使用内存映射? (http://docs.python.org/library/mmap.html) (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. 如果只读取一次文件的单个部分,可能会带来不必要的复杂性,但是您将要进行大量的IO工作,这会使管理起来更加容易。
from the python docs: 来自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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.