vendredi 27 février 2015

How can I convert a string into a function in Python?

I have to deal with csv image data from a camera which exports the data with a header. In that header is a simple function for converting CCD counts into power density. This equation includes both the dark offset level as well as a calibration factor. Here is an example from one line of an image file:



Power Density,=,(n - 232) * 4.182e-005 W/cm^2



Notice the commas. The csv header can be expected to have the same structure each time with different constants for dark level (232) and power density conversion (4.182e-005).


What I would like to be able to do is grab the last cell, strip off the units at the end (W/cm^2), and use what is left to define a function in Python. Something like



f = lambda n: '(n - 232) * 4.182e-005'


Is it possible to do so? If so, how?


Aucun commentaire:

Enregistrer un commentaire