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.
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?
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.
You will want to open the file then fileobj.seek(beginPos)
and then fileobj.read(endPos-beginPos)
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()
精彩评论