1、手动访问迭代器中的元素
#要手动访问迭代器中的元素,可以使用next()函数 In [3]: with open('/etc/passwd') as f: ...: try: ...: while True: ...: print(next(f)) #next()函数访问迭代中的函数 ...: except StopIteration: #捕获结束异常 ...: print('None') #通过指定返回结束值来判断迭代结束 In [28]: with open('/etc/passwd') as f: ...: while True: ...: line = next(f,None) ...: if line is None: ...: break ...: print(line)