Python 读取大文件
2016-01-07 15:51
405 查看
最近在用python处理日志分析数据,但是有些文件比较大,几个G,如果用linecache 或 都直接open整个文件,readlines 都容易导致占用过多内存,
导致程序停止执行。
Google了一下,python读取大文件的方法有以下几种:
个人推荐第一种,实测程序跑得刚刚的。
一、 用with 读取大文件
这个方法是在Stackoverflow上找到,这位高手对
The
is raised in the inner block. The
an iterable, which automatically uses buffered IO and memory management so you don't have to worry about large files.
大意就是
二、使用fileinput 模块
示例代码如下:
第一种更Pythonic,也无需import,而且还能处理close和Exception,所以更推荐使用。
在文件处理时,读取整行,每行后会有一个换行符。python 其实也提供了一个方法,strip.
英文的意思如下:
vt. 剥夺;剥去;脱去衣服
n. 带;条状;脱衣舞
vi. 脱去衣服
line=line.strip('\n')
像这样,把换行符脱掉就OK了。
导致程序停止执行。
Google了一下,python读取大文件的方法有以下几种:
个人推荐第一种,实测程序跑得刚刚的。
一、 用with 读取大文件
with读取是非常Pythonic的方法,示例如下:
with open(filepath) as f: for line in f: <do something with line>
这个方法是在Stackoverflow上找到,这位高手对
with读取的解释是这样的:
The
withstatement handles opening and closing the file, including if an exception
is raised in the inner block. The
for line in ftreats the file object
fas
an iterable, which automatically uses buffered IO and memory management so you don't have to worry about large files.
大意就是
with负责处理open和close文件,包括抛出内部异常。而
for line in f将文件对象
f当做迭代对象,将自动处理IO缓冲和内存管理,这样你无需担心大文件的处理了。
二、使用fileinput 模块
示例代码如下:
import fileinput for line in fileinput.input(['sum.log']): print line
第一种更Pythonic,也无需import,而且还能处理close和Exception,所以更推荐使用。
在文件处理时,读取整行,每行后会有一个换行符。python 其实也提供了一个方法,strip.
英文的意思如下:
vt. 剥夺;剥去;脱去衣服
n. 带;条状;脱衣舞
vi. 脱去衣服
line=line.strip('\n')
像这样,把换行符脱掉就OK了。
相关文章推荐
- Python Json获取天气预报
- Python发送邮件
- 笨方法学Python(46-50)
- [Leetcode]@python 62. Unique Paths
- Python 对图片进行人脸识别
- 黑科技 Python脚本帮你找出微信上删除你好友的人
- [Leetcode]@python 61. Rotate List
- python re的几个函数
- numpy安装
- python 大数据分析
- Python学习笔记
- Python fabric实现远程操作和部署
- 5.1.4. List Comprehensions(列表解析)
- windows下,python+scrapy环境搭建
- python 线程使用
- python 学习笔记 if语句
- Python系列之 - python数据类型(二)
- visual studio 2015使用python tools远程调试maya 2016
- python 实例7 为rhel添加系统用户与登录密码,并给root 发送邮件
- 【Python那些事儿之七】Iterators详解