I'm looking for a python library which comes with support to convert numbers between various SI prefixes, for example, kilo to pico, nano to giga and so on.What would you recommend?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Dictionaries
If you don't want to use any 3rd-party library like the ones listed below, you can actually implement your own parsing function.
Use a dictionary to match up the prefixes to their values. I've done it for you already:
Then you can use regex (as described by my answer here) to search or parse the input and use the dictionary for getting the appropriate value.
Unum
Unum is well finished and thoroughly documented library.
Pros:
Cons:
M
,S
etc. in your namespace)Magnitude
You can also use Magnitude, another library. It supports all the kinds of SI unit prefixes you're talking about, plus it'll handle the parsing as well. From the site:
I ported a simple function (original C version written by Jukka “Yucca” Korpela) to Python for formatting numbers according to SI standards. I use it often, for example, to set tick labels on plots, etc.
You can install it with:
The source is available on GitHub.
Example usage:
I don't know if this is the best answer but it is working in my case. Feel free to verify my solution. I am working for first time with Python and constructive criticism is welcome... along with positive feedback :D
This is my code:
And this is how it works:
And the response is:
I don't know if anyone will find this helpful or not. I hope you do. I've posted here so the people who want help to see it also to give them an idea maybe they can optimize it :)
I know this is an old thread, but I'd just like to throw out a reference to a python library I wrote which handles all manner of prefix unit conversion handling
Here's the major feature list:
kB
toGiB
)1024 Bytes == 1KiB
)<<
,>>
,&
,|
,^
)QuantiPhy is a new package that converts to and from numbers with SI scale factors. It is often a better choice that the unit packages such as Unum and Magnitude that are heavier and focused on the units rather than the scale factors.
QuantiPhy provides Quantity, which is an object that combines a number with its unit of measure (the units are optional). When creating a quantity you can use SI unit prefixes. Once you have a Quantity you can use it in expressions, where it acts as a float. Or you can convert it to a string, in which case it uses the SI unit prefixes by default.
By default QuantiPhy uses the natural prefix when rendering to a string, which is probably what you want. But you can force it to render to a specific prefix using scaling:
In this case you must turn off SI unit prefixes to avoid getting multiple prefixes: '1 npg'.
A more natural example might be where you are converting units:
The last example shows that you can place your desired units in the format string after the type and the conversion will be done for you automatically.