栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

(Python)尽可能快地计算巨大(> 10GB)文件中的行数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(Python)尽可能快地计算巨大(> 10GB)文件中的行数

gnacio的答案)是正确的,但是如果您使用32位进程,则可能会失败。

但是也许逐块读取文件然后计算

n
每个块中的字符可能会很有用。

def blocks(files, size=65536):    while True:        b = files.read(size)        if not b: break        yield bwith open("file", "r") as f:    print sum(bl.count("n") for bl in blocks(f))

会做你的工作。

请注意,我没有以二进制形式打开文件,因此

rn
将会转换为
n
,从而使计数更加可靠。

对于Python 3,并使其更强大,以便读取具有各种字符的文件:

def blocks(files, size=65536):    while True:        b = files.read(size)        if not b: break        yield bwith open("file", "r",encoding="utf-8",errors='ignore') as f:    print (sum(bl.count("n") for bl in blocks(f)))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号