Anake
Anake

Reputation: 7669

Automatically process numbers in e (scientific) notation in python

I am reading in data files from a mass spectrometer and many of the numbers are in e form e.g.

4096.26 5.785e1
4096.29 5.784e1
4096.31 5.784e1
4096.33 5.784e1
4096.36 5.783e1

I am planning on using the split function to get the two numbers out, but I wanted to know is there a function to convert the second column into python floats? I know I could do it with regular expressions but thought there might be a better way

Thank you

Upvotes: 25

Views: 42110

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 602385

The float() constructor will accept strings in e notation:

>>> float("5.785e1")
57.85

So you can simply use map(float, line.split()) to convert a text line to a list of floats.

Upvotes: 39

Related Questions