473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

parsing engineering symbols

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
Dec 20 '05 #1
14 1135
Something like:

def cvt(input):
if input.lower().endswith('k'): return float(input[:-1])*1000
Dec 20 '05 #2

Suresh Jeevanandam wrote:
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


port this
http://api.rubyonrails.com/classes/A...ric/Bytes.html

Dec 20 '05 #3
On Tue, 20 Dec 2005 19:07:02 +0530, Suresh Jeevanandam <jm*******@gmail.com> wrote:
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
Dec 20 '05 #4
Suresh Jeevanandam wrote:
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/

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Chance favors the trained mind.
-- Louis Pasteur
Dec 20 '05 #5
Suresh Jeevanandam wrote:
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>

Dec 20 '05 #6

Exactly what I wanted.

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

regards,
Suresh
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>

Dec 21 '05 #7
On Tue, 20 Dec 2005 23:28:12 +0100, "Fredrik Lundh" <fr*****@pythonware.com> wrote:
Suresh Jeevanandam wrote:
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
Dec 21 '05 #8
On Wed, 21 Dec 2005 19:10:21 +0530 in comp.lang.python, Suresh
Jeevanandam <jm*******@gmail.com> wrote:

[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

--
Change is inevitable, progress is not.
Dec 21 '05 #9
Bengt Richter wrote:
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>

Dec 21 '05 #10
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

Dec 21 '05 #11
Jacob Rael wrote:
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

Dec 21 '05 #12
Wow!! 261 v. 75 lines!!

jr

Dec 22 '05 #13
Peter Hansen wrote:
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


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>

Dec 22 '05 #14
Fredrik Lundh wrote:
Peter Hansen wrote:

[the OP wrote]
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


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

Dec 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tuang | last post by:
I've been looking all over in the docs, but I can't figure out how you're *supposed* to parse formatted strings into numbers (and other data types, for that matter) in Python. In C#, you can say...
14
by: Viktor Rosenfeld | last post by:
Hi, I need to create a parser for a Python project, and I'd like to use process kinda like lex/yacc. I've looked at various parsing packages online, but didn't find anything useful for me: -...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
5
by: fabricemarchant | last post by:
Hi ! In order to parse Lisp - for example - input coming from a stream : I wonder if it could be useful to write a small pre-function that would take this input, examine each incoming char...
6
by: duncan.welch | last post by:
Hi, I've got a website that uses links for page positioning which works great about 99% of the time. For some reason, 1% of the time I'll get an error when parsing the querystring, as it's...
12
by: karoly.kiripolszky | last post by:
Helo ppl! At the job I was given the task to make a script to analyze C++ code based on concepts my boss had. To do this I needed to represent C++ code structure in Python somehow. I read the...
6
by: korovev76 | last post by:
Hello everybody, I'm new to python (...I work with cobol...) I have to parse a file (that is a dbIII file) whose stucture look like this: |string|, |string|, |string that may contain commas...
1
by: Thomas Voigt | last post by:
Hi, I would like to extract information about used symbols in object files. The objective is that we have some pretty old libraries most of which have been marked as deprecated over time and we...
5
by: Svenn Are Bjerkem | last post by:
On Jul 23, 1:03 pm, christopher.saun...@durham.ac.uk (c d saunter) wrote: As a start I want to parse VHDL which is going to be synthesised, and I am limiting myself to the entities and the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.