scp404 最近的时间轴更新
scp404

scp404

V2EX 第 434283 号会员,加入于 2019-08-06 20:10:22 +08:00
scp404 最近回复了
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   ·   我们的愿景   ·   实用小工具   ·   892 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 21:09 · PVG 05:09 · LAX 14:09 · JFK 17:09
Developed with CodeLauncher
♥ Do have faith in what you're doing.