2

我有一个烦人的输出文件,如下所示:

*** 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 中做到这一点?基本上我想忽略以*、着色和求解开头的行

4

8 回答 8

3
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)

假设您的文本在一个名为烦恼.txt 的文件中,并在文件 output.txt 中输出(过滤结果)

编辑

if 条件可以更改为:

if line.startswith("***") or line.startswith("Solving") or line.startswith("Coloring"):

编辑2

如果换行符是分隔符,你也可以这样做

input_stream_lines=f.readlines()
于 2013-04-23T14:16:27.593 回答
1

startswith方法可以帮助您:

海峡_ startswith ( prefix [, start [, end ]])
如果字符串以前缀开头,则返回 True,否则返回 False。prefix 也可以是要查找的前缀元组。使用可选开始,测试从该位置开始的字符串。使用可选结束,停止在该位置比较字符串。

下面是一个示例代码,它显示了一个循环遍历文本文件的行,只打印那些不以“ ***”和“ Solving”开头的行:

for l in open("myfile.txt").readlines():
  if not l.startswith("***") and not l.startswith("Solving"): print(l)
于 2013-04-23T14:14:58.233 回答
1
with open("input.txt") as f:
    for line in f:
        if not (line.startswith("***") or line.startswith("Solving")):
            print line
于 2013-04-23T14:21:30.790 回答
0
import re

with open('log.txt', 'r') as f:
    for line in f:
        if not re.match("^\*\*\*", line):
            print line,

Edit: Too Slow

于 2013-04-23T14:23:02.003 回答
0

如果您只想要一个单行将其过滤到不同的文件中,只需使用 grep:

grep -ve "^(Solving\|\*\*\*)" input.txt > output.txt

或者,startswith按照其他答案指出的那样进行调查。

于 2013-04-23T14:18:35.893 回答
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
于 2013-04-23T14:19:25.350 回答
0

这看起来像 FEniCS 输出。您可以通过声明来抑制不需要的消息:

set_log_level(ERROR)

在你的代码中。

于 2013-04-23T14:21:51.763 回答
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
于 2013-04-23T14:45:37.693 回答