<p>当且仅当文件与给定示例一致时,此方法效果良好</p>
<p><strong>注意:</strong></p>
<p><code>There may be a faster way if regex is used and it might also be simpler</code>但想用逻辑的方式来做</p>
<p><strong>代码:</strong></p>
<pre><code>inp=open("output.txt","r")
inp=inp.read().split("\n")
print inp
tempString=""
output=[]
w=0
for s in inp:
if s:
if any(c.isalpha() for c in s):
tempString=tempString+" "+s
else:
w=0
if tempString:
output.append(tempString.strip())
tempString=""
output.append(s)
else:
if tempString:
output.append(tempString.strip())
tempString=""
output.append(" ")
if tempString:
output.append(tempString.strip())
print "\n".join(output)
out=open("newoutput.txt","w")
out.write("\n".join(output))
out.close()
</code></pre>
<p><strong>输入:</strong></p>
^{pr2}$
<p><strong>输出:</strong></p>
<pre><code>1
17:02,111
Problem report related to 2 router
2
17:05,223
Restarting the systems
3
18:02,444
Must erase hard disk now due to compromised data
4
17:02,111
Problem report related to router
</code></pre>