python简单读取大文件的方法

时间:2022-03-27 04:45:30

本文实例讲述了python简单读取大文件的方法。分享给大家供大家参考,具体如下:

Python读取大文件(GB级别)采用的办法很简单:

?
1
2
3
with open(...) as f:
 for line in f:
  <do something with line>

例如:

?
1
2
3
with open(filepath,'r') as infile:
 for line in infile:
  print line

一切都交给python解释器处理,读取效率很高,且占用资源少。

*参考链接:How to read large file, line by line in python - Stack Overflow

希望本文所述对大家Python程序设计有所帮助。