在区域中查找最后一个字符串后添加新行



我有一个输入test.txt文件,其中的输出交错为#Expected(在*标题区域中找到包含1 1 1的最后一行之后

以及Python 3.6 中的这段代码

index = 0
insert = False
currentTitle = ""
testfile = open("test.txt","r")    
content = testfile.readlines()
finalContent = content
testfile.close()
# Should change the below line of code I guess to adapt
#titles = ["TitleX","TitleY","TitleZ"]   

for line in content:
index = index + 1
for title in titles:
if line in title+"n":    
currentTitle = line
print (line)
if line == "1 1 1 1n":
insert = True
if (insert == True) and (line != "1 1 1 1n"):
finalContent.insert(index-1, currentTitle[:6] + "2" + currentTitle[6:])
insert = False

f = open("test.txt", "w")
finalContent = "".join(finalContent)
f.write(finalContent)
f.close()

更新:

提供答案的实际输出

*Title Test
12125
124125
asdas 1 1 1 1 
rthtr 1 1 1 1 
asdasf 1 1 1 1 
asfasf 1 1 1 1 
blabla 1 1 1 1 
#Expected "*Title Test2" here <-- it didn't add it
124124124
*Title Dunno
12125
124125
12763125 1 1 1 1 
whatever 1 1 1 1
*Title Dunno2
#Expected "*Title Dunno2" here <-- This worked great
214142122
#and so on for thousands of them..

还有没有一种方法可以在test.txt文件中覆盖它?

因为您已经将整个文件读入内存,所以很容易扫描两次行;一次是在每个标题之后找到区域的最后一个转换,一次是将修改后的数据写回相同的文件名,覆盖以前的内容。

我引入了一个字典变量transitions,其中键是具有转换的行的索引,每个行的值是在该点添加的文本。

transitions = dict()
in_region = False
reg_end = -1
current_title = None
with open("test.txt","r") as testfile:
content = testfile.readlines()
for idx, line in enumerate(content):
if line.startswith('*Title '):
# Commit last transition before this to dict, if any
if current_title:
transitions[reg_end] = current_title
# add suffix for printing
current_title = line.rstrip('n') + '2n'
elif line.strip().endswith(' 1 1 1 1'):
in_region = True
# This will be overwritten while we remain in the region
reg_end = idx
elif in_region:
in_region = False
if current_title:
transitions[reg_end] = current_title
with open("test.txt", "w") as output:
for idx, line in enumerate(content):
output.write(line)
if idx in transitions:
output.write(transitions[idx])

这种";还记得我们最后一次看到什么";循环很常见,但需要一些时间来适应。在循环中,请记住我们正在循环所有的行,并记住我们在这个循环的前一次迭代中看到的一些东西。(当你最终脱离循环时,忘记了你应该记住的最后一件事也是一个非常常见的错误!(

在我们寻找1 1 1 1之前,strip()通过移除任何周围的空白来规范输入。你也可以做其他类型的正常化;规范化数据是简化逻辑的另一种非常常见的技术。

演示:https://ideone.com/GzNUA5

使用itertools.zip_longest尝试此操作

from itertools import zip_longest
with open("test.txt","r") as f:
content = f.readlines()
results, title = [], ""
for i, j in zip_longest(content, content[1:]):
# extract title.
if i.startswith("*"):
title = i
results.append(i)
# compare value in i'th index with i+1'th (if mismatch add title)
if "1 1 1 1" in i and "1 1 1 1" not in j:
results.append(f'{title.strip()}2n')
print("".join(results))

最新更新