==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 模块详解的更多相关文章

  1. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  2. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  5. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  6. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  7. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

  8. python标准库介绍——22 UserList 模块详解

    ==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...

  9. python标准库介绍——21 UserDict 模块详解

    ==UserDict 模块== ``UserDict`` 模块包含了一个可继承的字典类 (事实上是对内建字典类型的 Python 封装). [Example 2-15 #eg-2-15] 展示了一个增 ...

随机推荐

  1. Sql Server-查询一列的数据进行拼接

    select convert(VARCHAR(10),memberid) +',' from t_member where teamid = 1009 for xml path('')

  2. 数据需求统计常用awk命令

    原文:http://www.5iops.com/html/2013/script_0418/267.html 1.将时间转换为时间戳 select unix_timestamp('2009-10-26 ...

  3. tcpdump-根据IP查看程序与服务都用了哪些端口

    tcpdump -i em1 -tttt src 116.3.248.157 and port ! 6869 -nn -i 指定端口 -tttt 附带时间戳 -nn 解析域名与端口信息 ####### ...

  4. Android ShareSDKQQ 第三方登录so easy?

    昨天群里有个群友看到我之前做的那个qq第三方登录怎么做的,于是乎思考了一下,还是决定写一篇博客记录下.事实上都不难的,事实上之前我又写到FaceBook的第三方登录不知道看下这Android集成Fac ...

  5. STL - 容器 - Array

    Array是C++ 11给STL新增加的容器 ArrayTest.cpp #include <array> #include <algorithm> #include < ...

  6. core dump相关

    linux下生成core dump文件方法及设置 http://www.2cto.com/os/201310/253450.html 在linux平台下,设置core dump文件生成的方法:   1 ...

  7. python mongodb ubuntu

    mongodb install: sudo apt-get install mongodb Install pip 1. $ sudo apt-get install python-pip pytho ...

  8. secureCRT简单设置(学习笔记二)

    菜鸟记录. 一.更改终端类型 选项-全局选项-默认会话-编辑默认设置-终端-仿真-右侧选择类型,下方设置缓冲区大小 二.设置字体和外观 上方窗口外观-右侧设置-字体设置字体类型大小,下面光标可以设置光 ...

  9. client高性能组件化框架React简单介绍、特点、环境搭建及经常使用语法

    [本文源址:http://blog.csdn.net/q1056843325/article/details/54729657 转载请加入该地址] 明天就是除夕了 预祝大家新春快乐 [ ]~( ̄▽ ̄) ...

  10. 位图算法 C语言

    #include <stdio.h> void set_bit(void *base, unsigned long n) { unsigned long *m = (unsigned lo ...