python - 防止 itertools.islice 修改行
问题描述
当逐行读取文件时,如果找到一个字符串,它将在之前和之后打印多行。但是,当我抓住这些线条之后,它会删除它们。我该怎么做才能使读取的行不受影响
data.txt
----------------------------
apple
banana
carrot
dog
egg
food
george
hat
ink
jacket
kiwi
lemon
mango
import itertools
import collections
with open("text", "r") as f:
linesBefore = collections.deque(maxlen=4)
for line in f:
line.rstrip()
if "george" in line:
history = list(map(str.strip, linesBefore))
history.append(line.rstrip())
append = list(itertools.islice(f, 4))
append = list(map(str.strip, append))
linesBefore.append(line)
print(line.rstrip())
#print('\n'.join(history))
#print('\n'.join(append))
正如您在打印行时所看到的,您可以看到在字符串“george”不在输出中之后抓取的文本
apple
banana
carrot
dog
egg
food
george
lemon
mango
解决方案
next()
您可以通过在看到目标行时保存和恢复文件的当前位置来做到这一点 - 但是您不能在 using 时使用(直接或间接通过for line in file:
or itertools.islice()
)遍历文件seek()
,因此您必须以不同的方式读取这些行。这实际上很简单:
import collections
with open("data_text.txt", "r") as f:
linesBefore = collections.deque(maxlen=4)
while True:
line = f.readline().rstrip()
if not line:
break
if "george" in line:
posn = f.tell() # Save where next line starts.
history = list(map(str.strip, linesBefore))
history.append(line.rstrip())
append = [f.readline() for _ in range(4)]
append = list(map(str.strip, append))
f.seek(posn) # Retore file position to where following line began.
linesBefore.append(line)
print(line.rstrip())
如果您使用的是 Python 3.8+,则可以使用该版本中添加的“海象”赋值运算符稍微简化循环。
import collections
with open("data_text.txt", "r") as f:
linesBefore = collections.deque(maxlen=4)
while (line := f.readline().rstrip()):
if "george" in line:
posn = f.tell() # Save where next line starts.
history = list(map(str.strip, linesBefore))
history.append(line.rstrip())
append = [f.readline() for _ in range(4)]
append = list(map(str.strip, append))
f.seek(posn) # Retore file position to where following line began.
linesBefore.append(line)
print(line.rstrip())
推荐阅读
- sql - PostgreSQL Query GROUP BY Year/Month - 没有函数与给定名称匹配?
- xcode - Xcode 11 beta 6 在 Xcode 11 beta 7 下载链接后面?
- jquery - 使用 jquery validate 验证是否选中了至少一个复选框
- ios - 如何使用 iOS 13 中的大标题对齐 NavigationBarItems?
- angular - 服务器端渲染时Angular获取元素客户端高度
- typescript - 如何定义允许嵌套属性键的类型
- rust - 在什么情况下是弧
> 不是在 Rust 中跨线程共享数据的最佳方式吗? - sql-server - 查询永远不会结束
- r - 使用 purrr 更改列表中的变量
- shell - 我正在尝试为行的开头和行的结尾添加反引号,该行的末尾有特殊字符