如何在Python中读取大文件的特定部分 [英] How to read specific part of large file in Python

查看:379
本文介绍了如何在Python中读取大文件的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个大文件(数百MB),我将如何使用Python快速读取文件中特定起始索引和结束索引之间的内容?

Given a large file (hundreds of MB) how would I use Python to quickly read the content between a specific start and end index within the file?

本质上,我正在寻找一种更有效的方法:

Essentially, I'm looking for a more efficient way of doing:

open(filename).read()[start_index:end_index]

推荐答案

您可以将文件seek放入文件,然后从那里读取一定数量. Seek允许您获取文件内的特定偏移量,然后可以将读取限制为该范围内的字节数.

You can seek into the file the file and then read a certain amount from there. Seek allows you to get to a specific offset within a file, and then you can limit your read to only the number of bytes in that range.

with open(filename) as fin:
    fin.seek(start_index)
    data = fin.read(end_index - start_index)

那只会读取您要查找的数据.

That will only read that data that you're looking for.

这篇关于如何在Python中读取大文件的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆