python标准库介绍——19 mmap 模块详解
==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示.
映射区域的行为和字符串对象类似, 但数据是直接从文件读取的. ====Example 2-13. 使用 mmap 模块====[eg-2-13] ```
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)) *B*<mmap object at 008A2A10>
302 302
'We will pe' 'We will pe'
'We will pe' 'rhaps even'*b*
``` 在 Windows 下, 这个文件必须以既可读又可写的模式打开( `r+` , `w+` , 或 `a+` ), 否则 ``mmap`` 调用会失败. ``` [!Feather 注: 经本人测试, a+ 模式是完全可以的, 原文只有 r+ 和 w+] %% WindowsError: [Error 5] Access is denied 一般是这个错误 [Example 2-14 #eg-2-14] 展示了内存映射区域的使用, 在很多地方它都可以替换普通字符串使用,
包括正则表达式和其他字符串操作. ====Example 2-14. 对映射区域使用字符串方法和正则表达式====[eg-2-14] ```
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() *B*43 'only small\015\012modules '
43 small*b*
```
python标准库介绍——19 mmap 模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- python标准库介绍——22 UserList 模块详解
==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...
- python标准库介绍——21 UserDict 模块详解
==UserDict 模块== ``UserDict`` 模块包含了一个可继承的字典类 (事实上是对内建字典类型的 Python 封装). [Example 2-15 #eg-2-15] 展示了一个增 ...
随机推荐
- (LeetCode 41)First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- YII插件
dropDownList: Yii中可以采用CHtml类来实现,也可以用CActiveForm来实现.一.用CHtml来实现.VIEW中实现: <?php echo CHtml::dropDow ...
- android触控,先了解MotionEvent
MotionEvent源代码可以在ocs看到,当然你也可以在SDK中下载源代码,或者其他地方,如: https://github.com/CyanogenMod/android_frameworks_ ...
- ZH奶酪:VirtualBox虚拟机与主机ping不通
6.进入虚拟机的Ubuntu(以下简称VBUbuntu),在VBUbuntu中用ifconfig查看ip地址,在Windows7中用ipconfig查看ip地址. 在VBUbuntu中ping Win ...
- MVC 之 属性详解
一.System [AttributeUsage]:指定另一特性类的用法.无法继承此类. [CLSCompliant]:指示程序元素是否符合公共语言规范 (CLS).无法继承此类. [ContextS ...
- UILabel文字竖排
方法一: UILabel *mindName = [[UILabel alloc]initWithFrame:kCR(, , ,)]; mindName.text = @"苏\n小\n明&q ...
- HTML二(基本标签)
一.标题 HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <!--标题--> <h1>标题 1</h1> ...
- Performance Testing
To test application performance, add rules using FiddlerScript to the OnBeforeResponse function (exc ...
- SSO是什么?
SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信仸的应用系统.它包括可以将这次主要的登录映射到其他应用中用亍同一个用户的登录的机制 ...
- SettingsEclipse
迁移时间--2017年5月20日08:45:07 CreateTime--2016年11月15日11:07:44Author:Marydon --------------------------- ...