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]

解决方案

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读取大文件的坑_如何在Python中读取大文件的特定部分相关推荐

  1. linux中文件重命名_如何在Linux中重命名文件?

    linux中文件重命名 This tutorial will walk you through the simple steps to rename a file in Linux. 本教程将引导您完 ...

  2. matlab分析xml文件_如何在Java中读取XML文件(DOM分析器)

    matlab分析xml文件 Today we will learn how to read the XML file in Java. We will also learn how to parse ...

  3. git 还原文件到其他版本_如何在Git中还原旧文件版本

    git 还原文件到其他版本 读: 第1部分:什么是Git? 第2部分:Git入门 第3部分:创建第一个Git存储库 第4部分:如何在Git中还原旧文件版本 第5部分:3个用于Git的图形工具 第6部分 ...

  4. java怎么引入html文件路径_如何在public_html中读取文件但在域外?使用相对路径...

    我正在尝试从我的(附加组件)域目录之外的目录中读取文件 . 这是我的目录结构: public_html / domain / file_read.php file_write.php sensitiv ...

  5. linux中python如何调用matlab的数据_?如何在Python中加入多个数据帧?

    全文共2235字,预计学习时长7分钟 来源:Pexels 初学Python编程的人,面临的是各种未知的挑战. 下面是一个几乎让所有更有抱负的数据科学家都感到意外的场景: 你正在处理一个从多个源收集数据 ...

  6. python set 排序_python set 排序_如何在Python中使用sorted()和sort()

    点击"蓝字"关注我们 ?"Python基础知识" 大卫·丰达科夫斯基  著 18财税3班 李潇潇    译 日期:2019年5月6日 一. 使用sorted() ...

  7. outlook附件大小限制_如何在Outlook中调整大图片附件的大小

    outlook附件大小限制 When you have to send someone a large image file through email, it's a good idea to re ...

  8. macos 虚拟镜像文件_如何在macOS中使用虚拟文件测试网络或硬盘速度

    macos 虚拟镜像文件 File transfer speeds can vary greatly from device to device. The same holds true for ne ...

  9. python一行输出多个数据_如何在Python中让两个print()函数的输出打印在一行内?

    1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用','代替空格,且默认末尾加 ...

最新文章

  1. Cisco防火墙技术汇总[转贴]
  2. linux slocate(secure locate) 命令详解
  3. 开源配置管理平台-Apollo
  4. HTTP、TCP、UDP,Socket,HTTPS
  5. ArcGIS 10.6 Data Interoperability Tools的安装与使用(附安装包下载)
  6. 通过JNI使用C ++尖叫快速进行Lucene搜索
  7. 打开editor的接口讨论
  8. 阶乘数码(洛谷P1591题题解,Java语言描述)
  9. (31)css常用属性兼容问题—需要加前缀的属性总结
  10. Linux下最快速共享目录的方法
  11. 表单相关标签之textarea,select
  12. Go接口(Interface)的使用方法
  13. servlet request返回上下文路径的方法
  14. 天涯明月刀服务器维护,天涯明月刀手游维护时间表一览,维护什么时候结束?[多图]...
  15. opencv打开摄像头
  16. 沉默成本谬误_估计与沉没成本谬误
  17. SNMP识别负载均衡设备
  18. Virtual Box报错VT-x is not available (VERR_VMX_NO_VMX)--大踩坑(二)
  19. 蓝牙耳机无法打开计算机,电脑搜不到蓝牙耳机怎么回事_电脑搜不到蓝牙耳机的处理方法【图文】...
  20. 再厚的马赛克都能被扒干净?这款去码神器火了

热门文章

  1. 静态页面被拦截解决办法
  2. php通过ajax下载文件,通过Ajax如何请求下载Execl文件
  3. mysql 5.6加用户_Mysql 5.6添加修改用户名和密码的方法
  4. 远程ykvm 插件移值java_Centos7 命令行下kvm安装windows,linux
  5. python中发送带附件的邮件,python SMTP实现发送带附件电子邮件
  6. 如何在android进行ltp测试,Android系统完整性度量架构IMA-EVM
  7. 如何用python创建文件_如何使用Python创建新的文本文件
  8. LeetCode算法入门- Remove Nth Node From End of List -day17
  9. uniapp打包成html5包个ios壳,HBuilder之uni-app打包App方法
  10. 轮询数据库 java_谁做过定时任务,轮询查询数据。