extract certain values from file with re
Paul McGuire
ptmcg at austin.rr._bogus_.com
Fri Oct 6 14:56:28 EDT 2006
"Fabian Braennstroem" <f.braennstroem at gmx.de> wrote in message
news:mailman.1438.1160128294.10491.python-list at python.org...
> Hi,
>
> I would like to remove certain lines from a log files. I had
> some sed/awk scripts for this, but now, I want to use python
> with its re module for this task.
>
> Actually, I have two different log files. The first file looks
> like:
>
> ...
> 'some text'
> ...
>
> ITER I----------------- GLOBAL ABSOLUTE RESIDUAL -----------------I
> I------------ FIELD VALUES AT MONITORING LOCATION ----------I
> NO UMOM VMOM WMOM MASS T EN DISS ENTH
> U V W P TE ED T
> 1 9.70E-02 8.61E-02 9.85E-02 1.00E+00 1.61E+01 7.65E+04 0.00E+00
> 1.04E-01-8.61E-04 3.49E-02 1.38E-03 7.51E-05 1.63E-05 2.00E+01
> 2 3.71E-02 3.07E-02 3.57E-02 1.00E+00 3.58E-01 6.55E-01 0.00E+00
> 1.08E-01-1.96E-03 4.98E-02 7.11E-04 1.70E-04 4.52E-05 2.00E+01
> 3 2.64E-02 1.99E-02 2.40E-02 1.00E+00 1.85E-01 3.75E-01 0.00E+00
> 1.17E-01-3.27E-03 6.07E-02 4.02E-04 4.15E-04 1.38E-04 2.00E+01
> 4 2.18E-02 1.52E-02 1.92E-02 1.00E+00 1.21E-01 2.53E-01 0.00E+00
> 1.23E-01-4.85E-03 6.77E-02 1.96E-05 9.01E-04 3.88E-04 2.00E+01
> 5 1.91E-02 1.27E-02 1.70E-02 1.00E+00 8.99E-02 1.82E-01 0.00E+00
> 1.42E-01-6.61E-03 7.65E-02 1.78E-04 1.70E-03 9.36E-04 2.00E+01
> ...
The pyparsing wiki includes an example
(http://pyparsing.wikispaces.com/space/showimage/dictExample2.py) for
parsing test data of the form:
+-------+------+------+------+------+------+------+------+------+
| | A1 | B1 | C1 | D1 | A2 | B2 | C2 | D2 |
+=======+======+======+======+======+======+======+======+======+
| min | 7 | 43 | 7 | 15 | 82 | 98 | 1 | 37 |
| max | 11 | 52 | 10 | 17 | 85 | 112 | 4 | 39 |
| ave | 9 | 47 | 8 | 16 | 84 | 106 | 3 | 38 |
| sdev | 1 | 3 | 1 | 1 | 1 | 3 | 1 | 1 |
+-------+------+------+------+------+------+------+------+------+
and accessing the parsed data (returned in the example in the variable
'data') as:
print "data keys=", data.keys()
print "data['min']=", data['min']
print "sum(data['min']) =", sum(data['min'])
print "data.max =", data.max
print "sum(data.max) =", sum(data.max)
print "data.columns =", data.columns
Giving:
data keys= ['ave', 'min', 'sdev', 'columns', 'max']
data['min']= [7, 43, 7, 15, 82, 98, 1, 37]
sum(data['min']) = 290
data.max = [11, 52, 10, 17, 85, 112, 4, 39]
sum(data.max) = 330
data.columns = ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2']
Not too disimilar from your example.
-- Paul
More information about the Python-list
mailing list