成都网站建设设计

将想法与焦点和您一起共享

创新互联Python教程:python3过大数据如何读取?

python中读取大文件的方法:

创新互联建站专业为企业提供垦利网站建设、垦利做网站、垦利网站设计、垦利网站制作等企业网站建设、网页设计与制作、垦利企业网站模板建站服务,十年垦利做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

1、利用yield生成器读取

def readPart(filePath, size=1024, encoding="utf-8"):
    with open(filePath,"r",encoding=encoding) as f:
        while True:
            part = f.read(size)  
            if part:
                yield part
            else:
                return None
filePath = r"filePath"
size = 2048 # 每次读取指定大小的内容到内存
encoding = 'utf-8'
for part in readPart(filePath,size,encoding):
    print(part)
    # Processing data

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

with open(filePath) as f:
    for line in f:
        print(line)
        # Processing data

当前标题:创新互联Python教程:python3过大数据如何读取?
网站地址:http://chengdu.cdxwcx.cn/article/dhchhge.html