我有一个讨厌的输出文件:
*** Warning: OpenMpAssembler::assemble_interior_facets is untested.
*** Warning: Form::coloring does not properly consider form type.
Coloring mesh.
*** Warning: Form::coloring does not properly consider form type.
Coloring mesh.
*** Warning: Form::coloring does not properly consider form type.
Solving linear system of size 108360 x 108360 (PETSc Krylov solver).
Solving linear system of size 36120 x 36120 (PETSc Krylov solver).
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
0/240 +0.000000e+00 -1.146247e-08 +0.000000e+00 +1.913417e-03 +0.000000e+00 +0.000000e+00
*** Warning: OpenMpAssembler::assemble_interior_facets is untested.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
Solving linear system of size 108360 x 108360 (PETSc Krylov solver).
Solving linear system of size 36120 x 36120 (PETSc Krylov solver).
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
1/240 +5.000000e-05 -2.024630e-08 -3.666293e-08 +1.913417e-03 +1.073840e-10 +3.479053e-21
*** Warning: OpenMpAssembler::assemble_interior_facets is untested.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
Solving linear system of size 108360 x 108360 (PETSc Krylov solver).
Solving linear system of size 36120 x 36120 (PETSc Krylov solver).
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
*** Warning: Form::coloring does not properly consider form type.
2/240 +1.000000e-04 -3.529024e-08 -3.825476e-08 +1.913417e-03 +1.077877e-10 +2.308941e-20
列表继续,我只想提取
0/240 +0.000000e+00 -1.146247e-08 +0.000000e+00 +1.913417e-03 +0.000000e+00 +0.000000e+00
1/240 +5.000000e-05 -2.024630e-08 -3.666293e-08 +1.913417e-03 +1.073840e-10 +3.479053e-21
2/240 +1.000000e-04 -3.529024e-08 -3.825476e-08 +1.913417e-03 +1.077877e-10 +2.308941e-20
我如何用Python做到这一点?基本上我想忽略以 * ,着色和解决
开头的行答案 0 :(得分:1)
startswith
方法可以帮助您:
str 。以开头<(em>前缀 [,开始 [,结束] ])
如果字符串以前缀开头,则返回True,否则返回False。 前缀也可以是元组 要查找的前缀。使用可选的start,测试字符串从 那个位置。使用可选的结尾,停止比较字符串 位置。
以下示例代码显示了文本文件行的循环,仅打印那些不以“***
”和“Solving
”开头的代码:
for l in open("myfile.txt").readlines():
if not l.startswith("***") and not l.startswith("Solving"): print(l)
答案 1 :(得分:1)
f=open('annoying.txt','r')
input_stream=f.read()
output_stream=""
input_stream_lines=input_stream.split("\n")
for line in input_stream_lines:
if "***" in line or "Solving" in line or "Coloring" in line:
pass
else:
output_stream=output_stream+line+"\n"
g=open('output.txt','w')
g.write(output_stream)
假设您的文本位于名为annoying.txt的文件中,并在文件output.txt中输出(已过滤的结果)
修改强>
if条件可以更改为:
if line.startswith("***") or line.startswith("Solving") or line.startswith("Coloring"):
<强> EDIT2 强>
如果换行符是分隔符,您也可以
input_stream_lines=f.readlines()
答案 2 :(得分:1)
with open("input.txt") as f:
for line in f:
if not (line.startswith("***") or line.startswith("Solving")):
print line
答案 3 :(得分:0)
如果你只想要一个单行将其过滤到另一个文件中,只需使用grep:
grep -ve "^(Solving\|\*\*\*)" input.txt > output.txt
或者,请查看其他答案指出的startswith
。
答案 4 :(得分:0)
使用此:
import re
f = open('badfile.txt', 'r')
result = 0
for line in f.readlines():
r = re.search(r'^\*\*\*|^Solving')
if r:
continue
result += line + '\n'
print result
答案 5 :(得分:0)
这看起来像FEniCS输出。您可以通过声明:
来抑制不需要的消息set_log_level(ERROR)
代码。
答案 6 :(得分:0)
import re
with open('log.txt', 'r') as f:
for line in f:
if not re.match("^\*\*\*", line):
print line,
编辑:太慢
答案 7 :(得分:0)
你可以这样做:
import re
from itertools import ifilter
with open('/tmp/annoying.txt','r') as f:
for line in ifilter(lambda x: re.match(r'\s+\d+',x),f):
print line.strip()
打印:
0/240 +0.000000e+00 -1.146247e-08 +0.000000e+00 +1.913417e-03 +0.000000e+00 +0.000000e+00
1/240 +5.000000e-05 -2.024630e-08 -3.666293e-08 +1.913417e-03 +1.073840e-10 +3.479053e-21
2/240 +1.000000e-04 -3.529024e-08 -3.825476e-08 +1.913417e-03 +1.077877e-10 +2.308941e-20