V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  scp404  ›  全部回复第 1 页 / 共 1 页
回复总数  2
2019-08-08 16:36:08 +08:00
回复了 scp404 创建的主题 Python 小白求助,在看《python3-cookbook》遇到的问题
@chenstack 我的错...代码贴错了,之前运行的时候变量名是对的。不过我把 txt 的内容换成老哥你的,能出结果。谢谢老哥了。
2019-08-08 15:43:25 +08:00
回复了 scp404 创建的主题 Python 小白求助,在看《python3-cookbook》遇到的问题
我的错...代码贴错了

直接 copy 书里的源码是这样的

```
from collections import deque


def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for line in lines:
if pattern in line:
yield line, previous_lines
previous_lines.append(line)

# Example use on a file
if __name__ == '__main__':
with open(r'../../cookbook/somefile.txt') as f:
for line, prevlines in search(f, 'python', 5):
for pline in prevlines:
print(pline, end='')
print(line, end='')
print('-' * 20)
```

没打印出东西

我是不是回复不了图片。。。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2383 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 126ms · UTC 10:06 · PVG 18:06 · LAX 03:06 · JFK 06:06
Developed with CodeLauncher
♥ Do have faith in what you're doing.