python3过大数据如何读取?
发布时间: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自学网
下一篇 python3中怎么编写类?
