用于升和毫升之间转换的Python单位模块不起作用

标签 python data-conversion

使用 'units 模块执行单位之间的基本转换。 然而,似乎不可能在升和毫升之间进行简单的转换...为什么?

>>> from units import unit

>>> one_liter = unit('L')(1)   
>>> one_liter
Quantity(1, LeafUnit('L', False))

>>> unit('mL')(one_liter)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/heinz/src/units/lib/python2.6/site-packages/units/abstract.py", line 23, in __call__
    raise IncompatibleUnitsError()
units.exception.IncompatibleUnitsError

最佳答案

您需要在 predefined 子模块中调用 define_units() 才能使用此包。

import units
import units.predefined

units.predefined.define_units()
one_liter = unit('L')(1)
unit('mL')(one_liter)

我认为这个包可以设计得更好 - 正如您所发现的,很容易意外地使用用户定义的单位而不是标准单位。

关于用于升和毫升之间转换的Python单位模块不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14169683/

相关文章:

python - $是什么意思?壳里的意思?

python - 避免三个 for 循环

c++ - 将字节数组转换为位图图像

c# - 如何将 html 文档保存到字符串中?

java - 日期转换jsp

python - 打开以两位数年份命名的文件-Python

python - 列表和 numpy 数组之间的比较

python - 重载加法、减法和乘法运算符

Hive - 将字符串转换为 bigint

javascript - 在 JavaScript 中从字符串中解码混合 ASCII 代码