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

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

问题描述

似乎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文件的"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行的最简洁方法是

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"").

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

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