[英]A way to find a word in a text document and delete the line with the word and the following 6 Lines (Python)
Is there a way to open a text file, find a word, delete the line with the word, and the following 6 lines of it?有没有办法打开一个文本文件,找到一个词,删除该词所在的行,以及下面的6行呢?
This should work这应该工作
def look_and_delete(file,word_to_look_for):
files = open(file,'r')
contents = files.readlines()
print(contents)
line = 0
file_words = ''
for i in contents:
print(i)
if word_to_look_for in i:
print("im in here on line", line)
else:
file_words+=i
line+=1
print(file_words)
files.close()
files = open(file,'w')
files.write(file_words)
files.close()
Fell free to remove the print statement they are irrelevant随意删除它们无关的打印声明
Here I have explain logic for file
editing with various section:在这里,我用各个部分解释了file
编辑的逻辑:
First we open file in read write mode
because r+ doesn't overwrite the previously written file
首先我们以read write mode
打开文件,因为r+ doesn't overwrite the previously written file
f = open("file.txt", 'r')
content = f.read()
Then we split
it into various line然后我们split
它分成不同的行
lines_ = content.split('\n')[::-1]
word = input('Enter your words : ')
Apply logic to remove maatch word line and 6 consecutive line.应用逻辑删除匹配字线和 6 个连续线。
for i, j in zip(range(len(lines_)), lines_):
if word in j:
for k in range(7):
if i > abs(k):
del lines_[i-k]
file_ = '\n'.join(lines_[::-1])
f.close()
Again we open file to write that updated string into file back that is why we use w write mode
which first clear the file then place string into it我们再次打开文件以将更新后的字符串写入文件,这就是我们使用w write mode
的原因,该模式首先清除文件然后将字符串放入其中
with open("file.txt", 'w') as f:
f.write(file_)
I would approach it in another way我会以另一种方式接近它
First open file and read lines for later, n=5
means - remove 5 lines including line with magic word.首先打开文件并读取行以备后用, n=5
表示 - 删除 5 行,包括带有魔法字的行。
magic_word = 'foo'
n = 5
with open('file.txt', 'r') as file_handle:
lines = file_handle.readlines()
num_lines = len(lines)
Next we enumerate over lines but in reverse.接下来我们逐行枚举,但反过来。 This is very important for a corner case, ex.:这对于极端情况非常重要,例如:
If we take this kind of input:如果我们采用这种输入:
test1
foo
test2
test3
test4
foo
test5
test6
test7
test8
Reading file from start to end (let n=5
) and looking for a magic word will result with output: output 从头到尾读取文件(让n=5
)并寻找一个神奇的词:
test1
test5
test6
test7
test8
But if we look and remove from the other end the result is:但是,如果我们从另一端查看并移除,结果是:
test1
That is why we need to revere the order.这就是为什么我们需要尊重秩序。
strip
is just so we can remove\n
before comparison.strip
只是为了让我们可以在比较之前删除\n
。
for idx, word in enumerate(reversed(lines)):
# now going from the end of a file
word = word.strip()
When we find magic word, we have to remove n
line from this point to file end.当我们找到魔法词时,我们必须从该点删除n
行到文件结尾。 Easiest operation to remove parts of a iterable in python is to slice删除 python 中可迭代部分的最简单操作是切片
ex.前任。
obj[1:5]
cut objects in range<1,5>
orobj[:5]
cut objects in range<0,5>
obj[1:5]
在<1,5>
范围内切割对象或obj[:5]
在<0,5>
范围内切割对象
if word == magic_word:
left_bound = num_lines-idx-1
if left_bound + n >= num_lines:
lines = lines[:left_bound]
else:
lines = lines[:left_bound] + lines[left_bound+n:]
All that is left is to save the file(override)剩下的就是保存文件(覆盖)
with open('file.txt', 'w') as file_handle:
lines = file_handle.writelines(lines)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.