擅长:python、mysql、java
<p>尝试迭代每一行,检查文本是否存在。你知道吗</p>
<p><strong>例如:</strong></p>
<pre><code>res = []
with open(filename, "r") as infile:
for line in infile:
if line.strip() == "Hi! How are you?":
res.append(line.strip())
lineVal = (next(infile)).strip()
if lineVal == "Can you hear me?":
res.append(lineVal)
res.append("\n Added new line \n")
else:
res.append(line.strip())
with open(filename1, "w") as out_file:
for line in res:
out_file.write(line+"\n")
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?
Added new line
this is very valuable
finish
</code></pre>