Text Processing
Alexander Kapps
alex.kapps at web.de
Tue Dec 20 19:01:41 EST 2011
On 20.12.2011 22:04, Nick Dokos wrote:
>>> I have a text file containing such data ;
>>>
>>> A B C
>>> -------------------------------------------------------
>>> -2.0100e-01 8.000e-02 8.000e-05
>>> -2.0000e-01 0.000e+00 4.800e-04
>>> -1.9900e-01 4.000e-02 1.600e-04
>>>
>>> But I only need Section B, and I need to change the notation to ;
>>>
>>> 8.000e-02 = 0.08
>>> 0.000e+00 = 0.00
>>> 4.000e-02 = 0.04
> Does it have to be python? If not, I'd go with something similar to
>
> sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}'
>
Why sed and awk:
awk 'NR>2 {printf("%.2f\n", $2);}' data.txt
And in Python:
f = open("data.txt")
f.readline() # skip header
f.readline() # skip header
for line in f:
print "%02s" % float(line.split()[1])
More information about the Python-list
mailing list