python读取大文件的坑_如何在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]
解決方案
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中读取大文件的特定部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 九宫怎么排列和使用_什么是九宫飞星,九宫
- 下一篇: python多标签分类_如何通过skle