python算法集锦【四】
利用生成器,在读取大文件时,可以为程序运行节省大量内存,例如:
1 def python_read(filename): 2 with open(filename,'r',encoding='utf-8') as f: 3 while True: 4 line = f.readline() 5 if not line: 6 return 7 yield line
利用生成器,在读取大文件时,可以为程序运行节省大量内存,例如:
1 def python_read(filename): 2 with open(filename,'r',encoding='utf-8') as f: 3 while True: 4 line = f.readline() 5 if not line: 6 return 7 yield line