Read a large file with python
python读取大文件
- 较pythonic的方法,使用with结构
- 文件可以自动关闭
- 异常可以在with块内处理
with open(filename, 'rb') as f:
for line in f:
<do someting with the line>
最大的优点:对可迭代对象 f,进行迭代遍历:for line in f,会自动地使用缓冲IO(buffered IO)以及内存管理,而不必担心任何大文件的问题。
There should be one – and preferably only one – obvious way to do it.
- 使用生成器generator
如果想对每次迭代读取的内容进行更细粒度的处理,可以使用yield生成器来读取大文件
def readInChunks(file_obj, chunkSize=2048):
"""
Lazy function to read a file piece by piece.
Default chunk size: 2kB.
"""
while True:
data = file_obj.read(chunkSize)
if not data:
break
yield data
f = open('bigFile')
for chunk in readInChunks(f):
do_something(chunk)
f.close()
- linux下使用split命令(将一个文件根据大小或行数平均分成若干个小文件)
wc -l BLM.txt # 读出BLM.txt文件一共有多少行
# 利用split进行分割
split -l 2482 ../BLM/BLM.txt -d -a 4 BLM_
# 将 文件 BLM.txt 分成若干个小文件,每个文件2482行(-l 2482),文件前缀为BLM_ ,系数不是字母而是数字(-d),后缀系数为四位数(-a 4)
# 按行数分割
split -l 300 large_file.txt new_file_prefix
# 文件大小分割
split -b 10m server.log waynelog
# 对文件进行合并:使用重定向,'>' 写入文件 , '>>' 追加到文件中
cat file_prefix* > large_file
在工作中的日常: 用户信息,log日志缓存,等都是大文件
补充:linecache模块
当读取一个文件的时候,python会尝试从缓存中读取文件内容,优化读取速度,提高效率,减少了I/O操作
linecache.getline(filename, lineno) 从文件中读取第几行,注意:包含换行符
linecache.clearcache() 清除现有的文件缓存
linecache.checkcache(filename=None) 检查缓存内容的有效性,可能硬盘内容发生改变,更新了,如果没有参数,将检查缓存中的所有记录(entries)
import linecache
linecache.getline(linecache.__file__, 8)
题目:
现给一个文件400M(该文件是由/etc/passwd生成的),统计其中root字符串出现的次数
import time
sum = 0
start = time.time()
with open('file', 'r') as f:
for i in f:
new = i.count('root')
sum+=new
end = time.time()
print(sum, end-start)
注:有时候这个程序比c,shell快10倍,原因就是,python会读取cache中的数据,使用缓存在内部进行优化,减少i/o,提高效率
References : How to read a large file
Read a large file with python的更多相关文章
- Read Large Files in Python
I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such ...
- Github Upload Large File 上传超大文件
Github中单个文件的大小限制是100MB,为了能突破这个限制,我们需要使用Git Large File Storage这个工具,参见这个官方帖子,但是按照其给的步骤,博主未能成功上传超大文件,那么 ...
- read content in a text file in python
** read text file in pythoncapability: reading =text= from a text file 1. open the IDLE text editor ...
- Combining an audio file with video file in python
Combining an audio file with video file in python - Stack Overflow https://stackoverflow.com/questio ...
- Java – Reading a Large File Efficiently--转
原文地址:http://www.baeldung.com/java-read-lines-large-file 1. Overview This tutorial will show how to r ...
- [PySpark] RDD programming on a large file
重难点 一.parallelize 方法 一般来说,Spark会尝试根据集群的状况,来自动设定slices的数目.然而,你也可以通过传递给parallelize的第二个参数来进行手动设置. data_ ...
- download file by python in google colab
https://stackoverflow.com/questions/15352668/download-and-decompress-gzipped-file-in-memory You need ...
- How do I get the path of the current executed file in Python?
First, you need to import from inspect and os from inspect import getsourcefile from os.path import ...
- Base64 encode/decode large file
转载:http://www.cnblogs.com/jzywh/archive/2008/04/20/base64_encode_large_file.html The class System.Co ...
随机推荐
- js堆栈内存的释放
### JS中的堆栈内存 > 俗称叫做作用域(全局作用域/私有作用域) > - 为js代码提供执行的环境(执行js代码的地方) > - 基本数据类型值是直接存放在栈内存中的 > ...
- 模拟代理安装---User-Agent Switcher for Chrome安装
模拟代理安装: 1. 百度User-Agent Switcher for Chrome进行下载 2. 直接安装 1. 进入扩展程序界面 2. 拖动插件进行安装 3.安装成功后会显示一个插件标志,里面可 ...
- Java实例---简单的个人管理系统
代码分析 FileOperate.java package com.ftl.testperson; import java.io.File ; import java.io.FileInputStre ...
- flash 动画展示
- hmac 算法模块
Hmac算法:Keyed-Hashing for Message Authentication.它通过一个标准算法,在计算哈希的过程中,把key混入计算过程中 Python自带的hmac模块实现了标准 ...
- 超强PHP集成环境,支持800多个不同PHP版本同时运行,无限自定义添加mysql与php版本
转载自今日头条 小编最近发现PHPWAMP竟然又更新了,激动之余向大家介绍一下新版的强大功能. PHPWAMP是纯绿色集成环境,高版本php无需安装VC运行库,默认已经集成VC运行库!Nginx支持服 ...
- 微信小程序及各种平台对接常用可逆加密算法aes256
不同程序之间经常会交换数据,我们经常采用的套路是: 假设要传输的信息是json,我们假设其为json_data,通过http传递信息为 json_data_encode=json_data&s ...
- Qt中限制IP输入的正则表达式:
这个例子中,是使用QLineEdit加入正则表达式来实现ip地址的输入功能的,不符合规范的数据将不能输入: QRegExp regExpIP("((25[0-5]|2[0-4][0-9]|1 ...
- 微服务、rest/restful、springmvc、http简要梳理
微服务简介 微服务是一个新兴的软件架构,就是把一个大型的单个应用程序和服务拆分为数十个的支持微服务.一个微服务的策略可以让工作变得更为简便,它可扩展单个组件而不是整个的应用程序.简而言之,微服务架构是 ...
- 有关js弹出提示框几种方法
1直接提示只有确定功能的提示框 只显示提示信息 alert(“提示信息”); alert ();的参数只有一个就是提示信息,无返回值 2 弹出输入框让你输入内容 prompt() ; 有两个参数:第一 ...