The mmap module
The mmap module
(New in 2.0) This module provides an interface to the operating system’s memory mapping functions. The mapped region behaves pretty much like a string object, but data is read directly from the file.
Example: Using the mmap module# File: mmap-example-1.py import mmap
import os filename = "samples/sample.txt" file = open(filename, "r+")
size = os.path.getsize(filename) data = mmap.mmap(file.fileno(), size) # basics
print data
print len(data), size # use slicing to read from the file
print repr(data[:10]), repr(data[:10]) # or use the standard file interface
print repr(data.read(10)), repr(data.read(10))$ python mmap-example-1.py
<mmap object at 008A2A10>
302 302
'We will pe' 'We will pe'
'We will pe' 'rhaps even'Under Windows, the file must currently be opened for both reading and writing (r+, or w+), or the mmap call will fail.
Memory mapped regions can be used instead of ordinary strings in many places, including regular expressions and many string operations:
Example: Using string functions and regular expressions on a mapped region# File: mmap-example-2.py import mmap
import os, string, re def mapfile(filename):
file = open(filename, "r+")
size = os.path.getsize(filename)
return mmap.mmap(file.fileno(), size) data = mapfile("samples/sample.txt") # search
index = data.find("small")
print index, repr(data[index-5:index+15]) # regular expressions work too!
m = re.search("small", data)
print m.start(), m.group()$ python mmap-example-2.py
43 'only small\015\012modules '
43 small
The mmap module的更多相关文章
- Modules you should know in Python Libray
前两天被问到常用的python lib和module有哪些?最常用的那几个,其他的一下子竟然回答不上.想想也是,一般情况下,遇到一个问题,在网上一搜,顺着线索找到可用的例子,然后基本没有怎么深究.结果 ...
- PHP APC缓存配置、使用详解
一.APC缓存简介 APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓 ...
- xlrd doc
The xlrd Module A Python module for extracting data from MS Excel ™ spreadsheet files. Version 0.7.3 ...
- 查看python中包的文档
核心命令:python -m pydoc 查询某包:python -m pydoc 包名 示例: C:\Users\xxx>python -m pydoc pydoc - the Python ...
- Perl语言入门:第九章 使用正则表达式处理文本 示例程序和代码
#! /usr/bin/perl use strict; use warnings; print "\n----------------------------------_substitu ...
- Python之mmap内存映射模块(大文本处理)说明
背景: 通常在UNIX下面处理文本文件的方法是sed.awk等shell命令,对于处理大文件受CPU,IO等因素影响,对服务器也有一定的压力.关于sed的说明可以看了解sed的工作原理,本文将介绍通过 ...
- Linux内存映射(mmap)系列(1)
看到同事的代码中出现了mmap.所以自己私下学习学习,研究研究..... http://www.cnblogs.com/lknlfy/archive/2012/04/27/2473804.html ( ...
- 【转】Python之mmap内存映射模块(大文本处理)说明
[转]Python之mmap内存映射模块(大文本处理)说明 背景: 通常在UNIX下面处理文本文件的方法是sed.awk等shell命令,对于处理大文件受CPU,IO等因素影响,对服务器也有一定的压力 ...
- 内核模块module传参
linux 2.6允许用户insmod的时候往内核模块里面传递参数,它主要使用module_param宏定义来实现这一功能. 参数应用 module_param(name, type, perm); ...
随机推荐
- 我的Android进阶之旅------>HTTP 返回状态值详解
(本文转载于:http://blog.csdn.net/ithomer/article/details/10240351) 当用户点击或搜索引擎向网站服务器发出浏览请求时,服务器将返回Http Hea ...
- 浅析SSH核心原理(二)
Hibernate是一个开放源代码的ORM(对象-关系映射)框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate可以应用在任 ...
- PHP - 接口 - 单一接口
/* * 接口的使用 */ //定义接口 interface IPerosn{ public function eat(); public function water(); } //定义继承自接口的 ...
- Win32 进程间通信的分析与比较(13种方法)
1 进程与进程通信 进程是装入内存并准备执行的程序,每个进程都有私有的虚拟地址空间,由代码.数据以及它可利用的系统资源(如文件.管道等)组成.多进程/多线 程是Windows操作系统的一个基本特征.M ...
- C#、ASP.NET、WinForm - 实现邮件发送的功能
转载自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html 发送邮件所用的核心知识点 微软封装好的Mail ...
- 【linux】内核编译
原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://blog.cs ...
- Oracle Patch Bundle Update
一.相关知识介绍 以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道还有个Bundle Patch,由于出现了TNS-12531的BU ...
- C#中Base64之编码,解码方法
原文:C#中Base64之编码,解码方法 1.base64 to string string strPath = "aHR0cDovLzIwMy44MS4yOS40Njo1NTU3L1 ...
- 14.9.2 Specifying the Row Format for a Table 指定 表的行格式
14.9.2 Specifying the Row Format for a Table 指定 表的行格式 mysql> SHOW TABLE STATUS\G; *************** ...
- PLSQL数据导入
导入数据 (1) 首先以管理员身份登录plsql; (2) 新建命令窗口 (3) 创建用户,设置帐号,密码 创建语句:create user usernameidenti ...