Astropy无法解析英寸

问题描述 投票:0回答:1

我试图用天蝎将inches转换为mm。在输入中,我将单位作为字符串(“英寸”,“mm”)。我为它创建了示例函数:

    def astro_conv(self, amount: float, fromm: str, to: str) -> float:
         u_from = u.Unit(fromm)
         u_to = u.Unit(to)
         return u_from.to(u_to, amount)

我收到消息:{ValueError}'inch' did not parse as unit: At col 0, inch is not a valid unit.我有检查文件和inch应该可用:http://docs.astropy.org/en/stable/units/我做错了什么?

python units-of-measurement astropy
1个回答
1
投票

Per the docs,Astropy不包括默认定义的英制单位。我认为这部分是为了减少默认单位名称空间以及创建和搜索它所涉及的开销,并且在这种情况下牺牲了英制单位,因为它们在天文学中的使用较少:

该软件包定义了常用的英制单位。它们在astropy.units.imperial命名空间中可用,但不在顶级astropy.units命名空间中,例如:

>>> import astropy.units as u
>>> mph = u.imperial.mile / u.hour
>>> mph
Unit("mi / h")

要将它们包括在撰写和find_equivalent_units的结果中,请执行以下操作:

>>> import astropy.units as u
>>> u.imperial.enable()  
© www.soinside.com 2019 - 2024. All rights reserved.