
with open(fileOne,"r") as First_file: for index,line in enumerate(First_file): # Do some stuff here with open(fileTwo,"r") as Second_file: for index,line in enumerate(Second_file): # Do stuff here aswell
问题是在第二个“with open”循环开始于文件的开头.因此,分析将花费很长时间.我也试过这个:
with open(fileOne,"r") as f1,open(fileTwo,"r") as f2: for index,(line_R1,line_R2) in enumerate(zip(f1,f2)):
问题是两个文件都直接加载到内存中.我需要每个文件中的相同行.正确的是:
number_line%4 == 1
这将给出第2,5,9,13行等.我需要两个文件中的那些行.
是否有更快的方式和更高效的内存方式?
解决方法 在Python 2中,使用itertools.izip()来防止文件被加载到内存中: from itertools import izipwith open(fileOne,line_R2) in enumerate(izip(f1,f2)):
内置的zip()函数确实会将两个文件对象完全读入内存,izip()一次检索一行.
总结以上是内存溢出为你收集整理的python同时逐行分析两个大文件全部内容,希望文章能够帮你解决python同时逐行分析两个大文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)