2012-06-10 29 views
4

我正在尋找一個支持將各種SI前綴之間的數字進行轉換的python庫,例如,kilo to pico,nano to giga等。您會推薦什麼?在SI單元前綴之間進行轉換的Python庫

+0

[Python中的單位轉換]可能的重複(http://stackoverflow.com/questions/2125076/unit-conversion-in-python) – GWW

+1

@GWW:不是真的,那個問題想要轉換單位,這是關於前綴。 – Junuxx

+0

@Zed:這是不是很清楚你想要做什麼。例如,輸入的格式是什麼?字符串?數字和字符串?數字和前綴索引?如果你舉了一個你想要做什麼的例子,這可能會有所幫助。 – Junuxx

回答

5

字典

如果你不希望使用任何第三方庫像下面列出的,實際上你可以實現你自己的分析功能。

使用字典將前綴與它們的值進行匹配。我已經做到了你已經:

_prefix = {'y': 1e-24, # yocto 
      'z': 1e-21, # zepto 
      'a': 1e-18, # atto 
      'f': 1e-15, # femto 
      'p': 1e-12, # pico 
      'n': 1e-9, # nano 
      'u': 1e-6, # micro 
      'm': 1e-3, # mili 
      'c': 1e-2, # centi 
      'd': 1e-1, # deci 
      'k': 1e3, # kilo 
      'M': 1e6, # mega 
      'G': 1e9, # giga 
      'T': 1e12, # tera 
      'P': 1e15, # peta 
      'E': 1e18, # exa 
      'Z': 1e21, # zetta 
      'Y': 1e24, # yotta 
    } 

然後你可以使用正則表達式(如my answer here描述)來搜索或解析輸入,並使用字典獲取適當的值。


UNUM

Unum是很好完成,全面記錄庫。

優點:

  • 允許定義任意單位(大小僅支持用戶定義的單元,只要它們是基本單元的組合)。

缺點:

  • 沒有處理好前綴
  • 雜波與所有單位定義命名空間(你結束了在你的命名空間命名爲MS等變量)

大小

您還可以使用Magnitude,另一個庫。它支持你正在談論的所有SI單位前綴,並且它還會處理解析。來自網站:

物理量是一個單位的數字,如10公里/小時。單位被指定爲字符串。它們可以是任何SI單位,加上一堆非SI,比特,美元和它們的任意組合。 它們可以包含標準的SI前綴。
...
從yocto到yotta和從kibi到exbi,所有標準前綴都被理解。

+0

正則表達式聽起來不像是檢測使用哪個前綴的最佳方式。不記錄(絕對值(值))可以在可用於選擇前綴的表單中爲您提供值的大小。 –

3

我不知道這是不是最好的答案,但它是工作在我的情況。隨時驗證我的解決方案。我第一次和Python一起工作,並且歡迎有建設性的批評...隨着正反饋:d
這是我的代碼:

class Units: 
def __init__(self): 
    global si; 
    si = { 
      -18 : {'multiplier' : 10 ** 18, 'prefix' : 'a'}, 
      -17 : {'multiplier' : 10 ** 18, 'prefix' : 'a'}, 
      -16 : {'multiplier' : 10 ** 18, 'prefix' : 'a'}, 
      -15 : {'multiplier' : 10 ** 15, 'prefix' : 'f'}, 
      -14 : {'multiplier' : 10 ** 15, 'prefix' : 'f'}, 
      -13 : {'multiplier' : 10 ** 15, 'prefix' : 'f'}, 
      -12 : {'multiplier' : 10 ** 12, 'prefix' : 'p'}, 
      -11 : {'multiplier' : 10 ** 12, 'prefix' : 'p'}, 
      -10 : {'multiplier' : 10 ** 12, 'prefix' : 'p'}, 
      -9 : {'multiplier' : 10 ** 9, 'prefix' : 'n'}, 
      -8 : {'multiplier' : 10 ** 9, 'prefix' : 'n'}, 
      -7 : {'multiplier' : 10 ** 9, 'prefix' : 'n'}, 
      -6 : {'multiplier' : 10 ** 6, 'prefix' : 'u'}, 
      -5 : {'multiplier' : 10 ** 6, 'prefix' : 'u'}, 
      -4 : {'multiplier' : 10 ** 6, 'prefix' : 'u'}, 
      -3 : {'multiplier' : 10 ** 3, 'prefix' : 'm'}, 
      -2 : {'multiplier' : 10 ** 2, 'prefix' : 'c'}, 
      -1 : {'multiplier' : 10 ** 1, 'prefix' : 'd'}, 
      0 : {'multiplier' : 1, 'prefix' : ''}, 
      1 : {'multiplier' : 10 ** 1, 'prefix' : 'd'}, 
      2 : {'multiplier' : 10 ** 3, 'prefix' : 'k'}, 
      3 : {'multiplier' : 10 ** 3, 'prefix' : 'k'}, 
      4 : {'multiplier' : 10 ** 3, 'prefix' : 'k'}, 
      5 : {'multiplier' : 10 ** 3, 'prefix' : 'k'}, 
      6 : {'multiplier' : 10 ** 6, 'prefix' : 'M'}, 
      7 : {'multiplier' : 10 ** 6, 'prefix' : 'M'}, 
      8 : {'multiplier' : 10 ** 6, 'prefix' : 'M'}, 
      9 : {'multiplier' : 10 ** 9, 'prefix' : 'G'}, 
      10 : {'multiplier' : 10 ** 9, 'prefix' : 'G'}, 
      11 : {'multiplier' : 10 ** 9, 'prefix' : 'G'}, 
      12 : {'multiplier' : 10 ** 12, 'prefix' : 'T'}, 
      13 : {'multiplier' : 10 ** 12, 'prefix' : 'T'}, 
      14 : {'multiplier' : 10 ** 12, 'prefix' : 'T'}, 
      15 : {'multiplier' : 10 ** 15, 'prefix' : 'P'}, 
      16 : {'multiplier' : 10 ** 15, 'prefix' : 'P'}, 
      17 : {'multiplier' : 10 ** 15, 'prefix' : 'P'}, 
      18 : {'multiplier' : 10 ** 18, 'prefix' : 'E'}, 
      } 

def convert(self, number): 
    # Checking if its negative or positive 
    if number < 0: 
     negative = True; 
    else: 
     negative = False; 

    # if its negative converting to positive (math.log()....) 
    if negative: 
     number = number - (number*2); 

    # Taking the exponent 
    exponent = int(math.log10(number)); 

    # Checking if it was negative converting it back to negative 
    if negative: 
     number = number - (number*2); 

    # If the exponent is smaler than 0 dividing the exponent with -1 
    if exponent < 0: 
     exponent = exponent-1; 
     return [number * si[exponent]['multiplier'], si[exponent]['prefix']]; 
    # If the exponent bigger than 0 just return it 
    elif exponent > 0: 
     return [number/si[exponent]['multiplier'], si[exponent]['prefix']]; 
    # If the exponent is 0 than return only the value 
    elif exponent == 0: 
     return [number, '']; 


,這是它如何工作的:

c1 = +1.189404E-010 
fres = -4.07237500000000E+007; 
ls = +1.943596E-005; 

units = sci.Units(); 
rValue, rPrefix = units.convert(c1); 
print rValue; 
print rPrefix; 

print units.convert(fres); 
print units.convert(ls); 

和響應是:

118.9404 
p 
[-40.72375, 'M'] 
[19.435959999999998, 'u'] 

我不知道是否有人會發現這有幫助或n OT。我希望你會。我已經在這裏發佈,所以需要幫助的人也可以給他們一個想法,也許他們可以優化它:)

4

我將一個簡單函數(original C version written by Jukka 「Yucca」 Korpela)移植到Python中,用於根據SI標準對數字進行格式化。我用它的時候,例如,對情節設置刻度標記等

你可以安裝它:

pip install si-prefix 

源可on GitHub

用法示例:

from si_prefix import si_format 

print si_format(.5) 
# 500.0m (default precision is 1) 

print si_format(.01331, precision=2) 
# 13.31m 

print si_format(1331, precision=2) 
# 1.33k 

print si_format(1331, precision=0) 
# 1k 
+0

你是一位金神 – Peter

1

我知道這是一個古老的線程,但我只是想拋出一個Python庫我寫了一個參考它處理處理

前綴單位轉換的所有方式

ħ ERE的主要功能列表:

0

QuantiPhy是一個新包,轉換和從與SI比例因子的數字。 Unum和Magnitude這類單位包比單位重,而不是比例因子,這通常是更好的選擇。

QuantiPhy提供數量,它是一個將數字與其度量單位相結合的對象(單位是可選的)。創建數量時,可以使用SI單位前綴。一旦你有一個數量,你可以在表達式中使用它,作爲一個浮點。或者您可以將其轉換爲字符串,在這種情況下,它默認使用SI單位前綴。

>>> from quantiphy import Quantity 

# convert strings to quantities 
>>> duration = Quantity('0.12 ks') 
>>> print(duration) 
120 s 

# convert to other units when rendering to a string 
>>> print(duration.render(scale='min')) 
2 min 

# quantities act like floats in expressions 
>>> rate = 1/duration 
>>> print(rate) 
0.008333333333333333 

# convert floats to quantities 
>>> rate = Quantity(rate, 'Hz') 
>>> print(rate) 
8.3333 mHz 

# can be used in format strings 
>>> print(f'Duration = {duration:<12.3} Rate = {rate}') 
Duration = 120 s  Rate = 8.3333 mHz 

默認情況下QuantiPhy渲染爲一個字符串,這可能是你想要什麼,當採用天然前綴。但是你可以強制其渲染爲一個特定的前綴使用比例:

>>> mass = Quantity('1000 g') 
>>> print(mass) 
1 kg 

>>> print(mass.render(show_si=False)) 
1e3 g 

>>> print(mass.render(show_si=False, scale=(1e-12, 'pg'))) 
1e9 pg 

在這種情況下,你必須關閉SI單位前綴,以避免越來越多的前綴:「1個NPG」。

一個更自然的例子可能是你在哪裏轉換單元:

>>> l = Quantity('2um')              
>>> print(l.render(scale='Å'))            
20 kÅ                   

>>> print(f'{l:sÅ}')               
20 kÅ 

最後一個例子表明,你可以把你想要的單位在格式字符串類型後,轉換會爲你自動完成。

相關問題