Text Processing

Yigit Turgut y.turgut at gmail.com
Thu Dec 22 06:11:13 EST 2011


On Dec 21, 2:01 am, Alexander Kapps <alex.ka... at web.de> wrote:
> 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])

@Jerome ; Your suggestion provided floating point error, it might need
some slight modificiation.

@Nick ; Sorry mate, it needs to be in Python. But I noted solution in
case if I need for another case.

@Alexander ; Works as expected.

Thank you all for the replies.



More information about the Python-list mailing list