自学Python之路-Python基础+模块+面向对象
自学Python之路-Python网络编程
自学Python之路-Python并发编程+数据库+前端
自学Python之路-djangohtml
此时在file文件里面输入内容,在生成器执行里面能够看到file的内容,且后续一直为空,程序一直在执行,只是读出的数据是空。前端
进一步,若是line不为空才打印python
f = open("file",encoding="utf-8") while True: line = f.readline() if line: print(line)
进一步,取消执行器看到的空格数据库
f = open("file",encoding="utf-8") while True: line = f.readline() if line: print(line.strip())
如何用生成器实现:
django
def tail(filename): f = open(filename,encoding="utf-8") while True: line = f.readline() if line.strip(): print(line.strip()) tail('file')
打印监听每行字前面加******编程
监听每行字若是有python才打印, 实现监听过滤功能。网络
def check_file(filename,aim): with open(filename,encoding='utf-8') as f: #句柄 : handler,文件操做符,文件句柄 for i in f: if aim in i: yield i g = check_file('test.01','生成器') for i in g: print(i.strip())
将文件test.01里面含有"生成器"的行数打印出来:并发
def check_file(filename): with open(filename,encoding='utf-8') as f: #句柄 : handler,文件操做符,文件句柄 for i in f: yield '***'+i for i in check_file('test.01'): print(i.strip())
......post