简体   繁体   English

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

[英]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.

相关问题 Python:获取两个给定子字符串之间的子字符串开始和结束 - Python: getting substring starting and ending between two given substrings 获取文本文件中开始和结束关键字之间的行,然后使用 python 进行后期处理 - Get lines between starting and ending keywords in text file, then do post processing using python Given a C++ file with many function definitions, how to get the starting and ending index of a particular function using Python? - Given a C++ file with many function definitions, how to get the starting and ending index of a particular function using Python? 使用 Python 读取具有给定结构定界的文本文件 - Read text file with a given structure delimitation with Python 如何使用开始和结束条件从文本中读取特定行? - How to read specific lines from text using a starting and ending condition? AttributeError: 'WebElement' object has no attribute 'get_text' 使用 Selenium Python 提取开始和结束标记之间的文本时出错 - AttributeError: 'WebElement' object has no attribute 'get_text' error extracting the text between the starting and ending tag using Selenium Python 如何在文本文件 python 中读取以特定 integer 开头的行? - how to Read a row starting with a specific integer in text file python? 给定特定单词在文本文件中的位置,请替换该单词(Python) - Replace a specific word given its position in a text file (Python) 如何将文件拆分为起始索引和结束索引之间的不规则部分? - How to split a file into irregular parts between the starting and ending indexes? Python正则表达式替换以特定短语开头和结尾的文本 - Python regex replace text starting and ending with specific phrases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM