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 14 1135
Something like:
def cvt(input):
if input.lower().endswith('k'): return float(input[:-1])*1000
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
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
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
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>
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>
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
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.
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>
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
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
Wow!! 261 v. 75 lines!!
jr
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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:
-...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |