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

Python-打开和更改大型文本文件

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

Python-打开和更改大型文本文件

您需要在每次迭代中读取一个咬合,对其进行分析,然后再写入另一个文件或中

sys.stdout
。试试这个代码:

mesh = open("file.mesh", "r")mesh_out = open("file-1.mesh", "w")c = mesh.read(1)if c:    mesh_out.write("{")else:    exit(0)while True:    c = mesh.read(1)    if c == "":        break    if c == "[":        mesh_out.write(",{")    elif c == "]":        mesh_out.write("}")    else:        mesh_out.write©

UPD:

它的工作速度非常慢(这要归功于jamylak)。所以我改变了它:

import sysimport redef process_char(c, stream, is_first=False):    if c == '':        return False    if c == '[':        stream.write('{' if is_first else ',{')        return True    if c == ']':        stream.write('}')        return Truedef process_file(fname):    with open(fname, "r") as mesh:        c = mesh.read(1)        if c == '': return        sys.stdout.write('{')        while True: c = mesh.read(8192) if c == '':     return c = re.sub(r'[', ',{', c) c = re.sub(r']', '}', c) sys.stdout.write(c)if __name__ == '__main__':    process_file(sys.argv[1])

因此,现在在1.4G文件上可以工作约15秒。要运行它:

$ python mesh.py file.mesh > file-1.mesh


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

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

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