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
Python has a defined syntax for representing a scientific notation. So, let us take a number of 0.000001234 then to represent it in a scientific form we write it as 1.234 X 10^-6. For writing it in python's scientific form we write it as 1.234E-6. Here the letter E is the exponent symbol.
Python uses special a syntax to write numbers in Scientific notation. For example, 0.000000123 can be written as 1.23E-7 . The letter E is called exponent and it doesn't matter whether you use e or E . Complex numbers are the numbers which we can't represent on a number line.
You can use E-notation to enter very big and very small numbers (or any number, for that matter) into Python. Later you'll see how to make Python print numbers using E-notation. Although we entered the numbers in E-notation, the answer came out as a regular decimal number.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With