如何从 mmapped 文件中读取行? [英] How to read lines from a mmapped file?

查看:23
本文介绍了如何从 mmapped 文件中读取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像mmap接口只支持readline().如果我尝试遍历对象,我会得到字符而不是完整的行.

Is seems that the mmap interface only supports readline(). If I try to iterate over the object I get character instead of complete lines.

逐行读取 mmap'ed 文件的pythonic"方法是什么?

What would be the "pythonic" method of reading a mmap'ed file line by line?

import sys
import mmap
import os


if (len(sys.argv) > 1):
  STAT_FILE=sys.argv[1]
  print STAT_FILE
else:
  print "Need to know <statistics file name path>"
  sys.exit(1)


with open(STAT_FILE, "r") as f:
  map = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
  for line in map:
    print line # RETURNS single characters instead of whole line

推荐答案

遍历 mmap 行的最简洁方法是

The most concise way to iterate over the lines of an mmap is

with open(STAT_FILE, "r+b") as f:
    map_file = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
    for line in iter(map_file.readline, b""):
        # whatever

请注意,在 Python 3 中,iter() 的 sentinel 参数必须是 bytes 类型,而在 Python 2 中它需要是 str(即 "" 而不是 b"").

Note that in Python 3 the sentinel parameter of iter() must be of type bytes, while in Python 2 it needs to be a str (i.e. "" instead of b"").

这篇关于如何从 mmapped 文件中读取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆