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); ...
随机推荐
- 【iOS开发-72】设置状态栏的两种方式、程序生命周期以及更好地理解几大类(对象)之间的关系
(1)设置状态栏的2种方式 --第一种方式就是我们在控制器中设置,系统默认就是交给视图控制器去管理的,这样不同视图控制器能够自己定义不同的状态栏例如以下: -(BOOL)prefersStatusBa ...
- “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册
原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...
- Excel 公式(细节若干)
查找与引用: 1.如果需要找出匹配元素的位置而不是匹配元素本身,则应该使用 MATCH 函数而不是 LOOKUP 函数. 2.VLOOKUP的第一个参数允许使用通配符“*”来表示包含的意思,把*放在字 ...
- WCF技术剖析之十五:数据契约代理(DataContractSurrogate)在序列化中的作用
原文:WCF技术剖析之十五:数据契约代理(DataContractSurrogate)在序列化中的作用 [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经> ...
- 利用WebClient上传参数及文件流到远程ashx服务
原文 利用WebClient上传参数及文件流到远程ashx服务 1 思路: WebClient.UploadFile()方法可以上传文件:UploadData()方法可以上传数据参数:如何合二为一既上 ...
- 基于visual Studio2013解决面试题之1003字符串逆序
题目
- 'datetime.datetime' has no attribute 'datetime'问题
写python时,用到了日期计算.于是写了datetime.datetime(*d_startTime[0:6])这样的代码. 结果编译不通过,报 'datetime.datetime' has no ...
- Eclipse用法和技巧十七:覆盖父类方法
在学校里面学习java,遇到访问权限修饰符一直停留在public是公有的,外面可以访问:protected是对子类可见的,外部不可以访问:private仅在本类中可见.工作之后,接触到了java代码多 ...
- Qt5.2 android 环境搭建及其测试
1.<安装> 软件: qt-windows-opensource-5.2.0-android-x86-win32-offline //Qt开发环境 adt-bundle-windows-x ...
- cocos2d-x游戏开发系列教程-坦克大战游戏启动界面的编写
用前面介绍的方法,创建一个cocos2d-x项目,可以看到新项目内容如下图: