我是靠谱客的博主 务实云朵,这篇文章主要介绍python读取文件内容的方法_Python读取文件内容的三种方式并比较,现在分享给大家,希望可以做个参考。

本次实验的文件是一个60M的文件,共计392660行内容。

525649-20160919190238231-1945770869.png

程序一:

defone():

start=time.clock()

fo= open(file,'r')

fc=fo.readlines()

num=0for l infc:

tup= l.rstrip('n').rstrip().split('t')

num= num+1fo.close()

end=time.clock()print end-startprint num

运行结果:0.812143868027s

程序二:

deftwo():

start=time.clock()

num=0

with open(file,'r') as f:for l inf:

tup= l.rstrip('n').rstrip().split('t')

num= num+1end=time.clock()

times= (end-start)printtimesprint num

运行时间:0.74222778078

程序三:

defthree():

start=time.clock()

fo= open(file,'r')

l=fo.readline()

num=0whilel:

tup= l.rstrip('n').rstrip().split('t')

l=fo.readline()

num= num+1end=time.clock()print end-startprint num

运行时间:1.02316120797

由结果可得出,程序二的速度最快。

最后

以上就是务实云朵最近收集整理的关于python读取文件内容的方法_Python读取文件内容的三种方式并比较的全部内容,更多相关python读取文件内容内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部