当前位置: 首页 > 图灵资讯 > 行业资讯> python3过大数据如何读取?

python3过大数据如何读取?

来源:图灵python
时间: 2026-01-05 22:25:38

在python中读取大文件的方法:

1、使用yield生成器读取

defreadPart(filePath,size=1024,encoding="utf-8"):
withopen(filePath,"r",encoding=encoding)asf:
whileTrue:
part=f.read(size)
ifpart:
yieldpart
else:
returnNone
filePath=r"filePath"
size=2048#每次读取指定大小的内容到内存
encoding='utf-8'
forpartinreadPart(filePath,size,encoding):
print(part)
#Processingdata

2、用open()自带的方法生成迭代对象,这是一行一行的读取

withopen(filePath)asf:
forlineinf:
print(line)
#Processingdata

更多Python知识请关注Python自学网