Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于在SI单位前缀之间转换的Python库_Python - Fatal编程技术网

用于在SI单位前缀之间转换的Python库

用于在SI单位前缀之间转换的Python库,python,Python,我正在寻找一个python库,它支持在各种SI前缀之间转换数字,例如,从kilo到pico,从nano到giga等等。您推荐什么?字典 如果您不想使用下面列出的任何第三方库,您实际上可以实现自己的解析功能 使用字典将前缀与其值进行匹配。我已经为你做了: \u前缀={'y':1e-24,#yocto “z”:1e-21,#zepto “a”:1e-18,#阿托 “f”:1e-15,#毫微秒 “p”:1e-12,#微微 “n”:1e-9,#nano “u”:1e-6,#微 “m”:1e-3,#mil

我正在寻找一个python库,它支持在各种SI前缀之间转换数字,例如,从kilo到pico,从nano到giga等等。您推荐什么?

字典 如果您不想使用下面列出的任何第三方库,您实际上可以实现自己的解析功能

使用字典将前缀与其值进行匹配。我已经为你做了:

\u前缀={'y':1e-24,#yocto
“z”:1e-21,#zepto
“a”:1e-18,#阿托
“f”:1e-15,#毫微秒
“p”:1e-12,#微微
“n”:1e-9,#nano
“u”:1e-6,#微
“m”:1e-3,#mili
“c”:1e-2,#厘米
“d”:1e-1,#分贝
“k”:1e3,千克
“M”:1e6,#兆
“G”:1e9,#千兆
“T”:1e12,#太拉
“P”:1e15,#善待动物组织
“E”:1e18,#exa
“Z”:1e21,#zetta
“Y”:1e24,#约塔
}
然后可以使用regex(如所述)搜索或解析输入,并使用字典获取适当的值


联合国大学 是一个完整的、完整的文档库

优点:

  • 允许您定义任意单位(幅值仅支持用户定义的单位,只要它们是基本单位的组合)
缺点:

  • 不能很好地处理前缀
  • 将名称空间与其所有单元定义混在一起(名称空间中的变量名为
    M
    S
    等)

量级 您还可以使用另一个库。它支持您所说的所有类型的SI单位前缀,而且它还可以处理解析。从网站:

物理量是以单位表示的数字,如10 km/h。单位被指定为字符串。它们可以是任何国际单位制,加上一堆非国际单位制、比特、美元以及它们的任意组合它们可以包括标准SI前缀。

从yocto到yotta,从kibi到exbi,所有标准前缀都可以理解。


我不知道这是否是最好的答案,但它在我的情况下是有效的。请随意验证我的解决方案。我是第一次使用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' : 'da'},
           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, ''];
答案是:

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

我不知道是否有人会觉得这有帮助。我希望你能。我在这里发布了一个帖子,希望得到帮助的人也可以给他们一个想法,也许他们可以优化它:)

我向Python移植了一个简单的函数,用于根据SI标准格式化数字。例如,我经常使用它在绘图上设置记号标签,等等

您可以通过以下方式安装它:

pip install si-prefix
来源是可用的

用法示例:

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

我知道这是一个旧线程,但我只想抛出一个对我编写的python库的引用,该库处理所有形式的前缀单元转换处理

以下是主要功能列表:

  • SINIST前缀单位之间转换(
    kB
    GiB
  • 在相同类型的单位之间转换(SI到SI,或NIST到NIST)
  • 自动人类可读的前缀选择(如in hury.filesize)
  • 基本算术运算(从50GiB中减去42KiB)
  • 丰富的比较操作(
    1024字节==1KB
  • 按位操作(
    &
    |
    ^
  • 读取设备的存储容量(仅限Linux/OS X支持)
  • argparse 作为自定义类型的集成
  • 进度条 集成为一个文件传输速度更快的小部件
  • 字符串解析
  • 分类
是一种新的软件包,它可以与具有SI比例因子的数字进行转换。通常更好的选择是,联合国大学和震级等单位包更重,侧重于单位而不是比额表因素

QuantiPhy提供Quantity,Quantity是一个将数字与其度量单位相结合的对象(单位是可选的)。创建数量时,可以使用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
在这种情况下,必须关闭SI单位前缀以避免获得多个前缀:“1 npg”

更自然的例子可能是转换单位:

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

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

最后一个示例显示,您可以将所需的单位放在类型后的格式字符串中,转换将自动完成。

可能重复@GWW:不太可能,该问题想要转换单位,这是关于前缀的。@Zed:您不太清楚要做什么。例如,输入的格式是什么?串?数字和字符串?数字和前缀索引?如果你能给出一个你想做什么的例子,可能会有所帮助。但是由于前缀的数量相当有限,你最好使用一个简单的字典(例如,
{'giga':1e9,'kilo':1e3,'milli':1e-3,…}
)Regex听起来并不是检测使用哪个前缀的最佳方法。日志(abs(value))不会以可用于选择前缀的形式为您提供值的大小。改进建议:您使用“d”作为deci和deca的前缀,这既令人困惑又不正确。Deca应具有前缀“da”。
>>> 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
>>> l = Quantity('2um')                                                      
>>> print(l.render(scale='Å'))                                               
20 kÅ                                                                        

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