Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically process numbers in e (scientific) notation in python

Tags:

python

numbers

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

like image 870
Anake Avatar asked Jun 27 '11 11:06

Anake


People also ask

How do you put numbers in scientific notation in Python?

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.

How do you do E notation in Python?

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.

Does Python support E notation?

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.


1 Answers

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.

like image 69
Sven Marnach Avatar answered Oct 20 '22 15:10

Sven Marnach