parsing engineering symbols

S

Suresh Jeevanandam

Hi,
I want to convert a string to float value. The string contains
engineering symbols.
For example,

s = '12k'

I want some function which would return 12000
function(s)
=> 12000.0
I searched the web, but could not find any function.

regards,
Suresh
 
L

Larry Bates

Something like:

def cvt(input):
if input.lower().endswith('k'): return float(input[:-1])*1000
.
. add your other special symbols here
.
return None # didn't find a match

Larry Bates
 
B

Bengt Richter

Hi,
I want to convert a string to float value. The string contains
engineering symbols.
For example,

s = '12k'

I want some function which would return 12000
function(s)
=> 12000.0
I searched the web, but could not find any function.
It should be easy enough, if you can define precisely what
"contains engineering symbols" means ;-)

Regards,
Bengt Richter
 
E

Erik Max Francis

Suresh said:
I want to convert a string to float value. The string contains
engineering symbols.
For example,

s = '12k'

I want some function which would return 12000
function(s)
=> 12000.0
I searched the web, but could not find any function.

There's some extensive code in the SI class in BOTEC which does this:

http://www.alcyone.com/software/botec/
 
F

Fredrik Lundh

Suresh said:
I want to convert a string to float value. The string contains
engineering symbols.

For example,

s = '12k'

I want some function which would return 12000
function(s)
=> 12000.0
I searched the web, but could not find any function.

how about:

SI_prefixes = {
'Y':24, 'Z':21, 'E':18, 'P':15, 'T':12, 'G':9, 'M':6, 'k':3,
'h':2, 'd':-1, 'c':-2, 'm':-3, u'\xb5':-6, 'u':-6, 'n':-9, 'p':-12,
'f':-15, 'a':-18, 'z':-21, 'y':-24
}

def myfloat(str):
try:
exp = SI_prefixes[str[-1]]
return float(str[:-1]) * 10**exp
except KeyError:
return float(str)

?

</F>
 
S

Suresh Jeevanandam

Exactly what I wanted.

It would be nice if the standard float function takes care of these.

regards,
Suresh
 
B

Bengt Richter

Suresh said:
I want to convert a string to float value. The string contains
engineering symbols.

For example,

s = '12k'

I want some function which would return 12000
function(s)
=> 12000.0
I searched the web, but could not find any function.

how about:

SI_prefixes = {
'Y':24, 'Z':21, 'E':18, 'P':15, 'T':12, 'G':9, 'M':6, 'k':3,
'h':2, 'd':-1, 'c':-2, 'm':-3, u'\xb5':-6, 'u':-6, 'n':-9, 'p':-12,
'f':-15, 'a':-18, 'z':-21, 'y':-24
}

def myfloat(str):
try:
exp = SI_prefixes[str[-1]]
return float(str[:-1]) * 10**exp
except KeyError:
return float(str)

?

Nit: why not move 10** out of myfloat into SI_prefixes
(i.e., just retrieve precalculated factors)?

Nit_2: No twinge of conscience shadowing str? ;-)

Regards,
Bengt Richter
 
D

Dave Hansen

On Wed, 21 Dec 2005 19:10:21 +0530 in comp.lang.python, Suresh

[re: SI prefixes]
Exactly what I wanted.

It would be nice if the standard float function takes care of these.

No, it wouldn't.

Regards,
-=Dave
 
F

Fredrik Lundh

Bengt said:
Nit: why not move 10** out of myfloat into SI_prefixes
(i.e., just retrieve precalculated factors)?

the table I cut and pasted from only listed the exponents. if you want
to prefix 1e to all the constants, be my guest.
Nit_2: No twinge of conscience shadowing str? ;-)

not the slightest.

</F>
 
J

Jacob Rael

Just a newbie, trolling.

I like this solution. Simple, easy to understand.

Did you put the SI_prefixes outside the def so you could wrap the
whole thing in a module and reuse it in other blocks?

jr
 
P

Peter Hansen

Jacob said:
Just a newbie, trolling.

I like this solution. Simple, easy to understand.

Did you put the SI_prefixes outside the def so you could wrap the
whole thing in a module and reuse it in other blocks?

Do "import dis" and then try "dis.dis(myfloat)" on Fredrik's version and
on your suggested variation... (Hint, it's likely more an instinct for
decent performance instead of an instinct for decent portability, though
it would serve both purposes.)

-Peter
 
F

Fredrik Lundh

Peter said:
Do "import dis" and then try "dis.dis(myfloat)" on Fredrik's version and
on your suggested variation... (Hint, it's likely more an instinct for
decent performance instead of an instinct for decent portability

I frankly don't see what "portability" has to do with this. Why would the
location of a reusable static data structure matter ? Python code work
the same way in scripts and modules...

</F>
 
P

Peter Hansen

Fredrik said:
Peter Hansen wrote: [the OP wrote]
Do "import dis" and then try "dis.dis(myfloat)" on Fredrik's version and
on your suggested variation... (Hint, it's likely more an instinct for
decent performance instead of an instinct for decent portability

I frankly don't see what "portability" has to do with this. Why would the
location of a reusable static data structure matter ? Python code work
the same way in scripts and modules...

A local variable (i.e. "inside the def") wouldn't be quite so accessible
as a global one...

At least, that's what I inferred the OP meant with his question above.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,368
Members
48,060
Latest member
JerrodSimc

Latest Threads

Top