scp404's recent timeline updates
scp404

scp404

V2EX member #434283, joined on 2019-08-06 20:10:22 +08:00
scp404's recent replies
@chenstack 我的错...代码贴错了,之前运行的时候变量名是对的。不过我把 txt 的内容换成老哥你的,能出结果。谢谢老哥了。
我的错...代码贴错了

直接 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)
```

没打印出东西

我是不是回复不了图片。。。
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   969 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 19ms · UTC 19:25 · PVG 03:25 · LAX 12:25 · JFK 15:25
♥ Do have faith in what you're doing.