Parse Python String for Units

百般思念 提交于 2020-02-26 12:13:46

问题


Say I have a set of strings like the following:

"5 m^2"
"17 sq feet"
"3 inches"
"89 meters"

Is there a Python package which will read such strings, convert them to SI, and return the result in an easily-usable form? For instance:

>>> a=dream_parser.parse("17 sq feet")
>>> a.quantity
1.5793517
>>> a.type
'area'
>>> a.unit
'm^2'

回答1:


More recently, pint is a good place to start for most of these.




回答2:


Quantulum will do exactly what you described

Excerpt from its description:

from quantulum import parser

quants = parser.parse('I want 2 liters of wine') 

# quants [Quantity(2, 'litre')] 



回答3:


Is there an extension for ipython that can do at least part of what you want. It's called ipython-physics

It does store value and units and allows (at least) some basic math. I have never used it myself, so I don't know how easy would be to use in a python script



来源:https://stackoverflow.com/questions/15161012/parse-python-string-for-units

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!