python读取大文件时避免内存溢出
大文件直接用read()函数会内存溢出
解决办法
1. 一行一行的取
with open('t1.txt') as f:
while True:
data = f.readline()
# 判断文件是否结束
if not data:
break
# 优化版
with open('t1.txt') as f:
data = f.readline()
while data:
data = f.readline()
2. 使用第三方模块linecache,可以取指定行
import linecache
# 读取第2行
data = linecache.getline('t1.txt',2)
3.使用for迭代
with open('t1.txt') as f:
for i in f:
print(i)
参考
https://www.cnblogs.com/misswangxing/p/7921740.html
https://blog.csdn.net/weixin_40006779/article/details/109879397