Parse Python String for Units
Asked Answered
P

4

12

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'
Plotinus answered 1/3, 2013 at 15:17 Comment(6)
pypi.python.org/pypi/units is good, but I don't think it'll parse the units out of a string for you.Ostracism
Here's another one: pint.readthedocs.org/en/latestSurfbird
possible duplicate of Python library to convert between SI unit prefixesSelfdelusion
This is a non-duplicate @BurhanKhalid. I am not interested in purely SI prefixes and do not assume that the units are already known by the programmer/user: string parsing is a must.Plotinus
I meant that the solutions in the answer could provide a starting point.Selfdelusion
@isedev, Pint has some of the features I'm looking for, though the string processing is a little more rudimentary than I'd like.Plotinus
D
6

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')] 
Dall answered 16/8, 2018 at 0:9 Comment(3)
This is great but it doesn't work for multi-dimensions like 273x27x27 mm...Dedication
Doesn't work with Python 3, last release was in August 2016.Reverence
There is a python 3 version, Quantulum3Nash
S
3

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

Soluble answered 20/4, 2015 at 16:26 Comment(0)
D
3

If you have 'nice' strings then use pint.

(best for unit conversions)

import pint
u = pint.UnitRegistry()
value = u.quantity("89 meters")

If you have text/sentences then use quantulum

from quantulum import parser

value = parser.parse('Pass me a 300 ml beer.') 

If you have 'ugly' strings then use try unit_parse.

Examples of 'ugly' strings: (see unit_parse github for more examples)

2.3 mlgcm --> 2.3 cm * g * ml
5E1 g/mol --> 50.0 g / mol
5 e1 g/mol --> 50.0 g / mol
()4.0 (°C) --> 4.0 °C
37.34 kJ/mole (at 25 °C) --> [[<Quantity(37.34, 'kilojoule / mole')>, <Quantity(25, 'degree_Celsius')>]]
Detection in water: 0.73 ppm; Chemically pure --> 0.73 ppm

(uses pint under the hood)

from unit_parse import parser

result = parser("1.23 g/cm3 (at 25 °C)")
print(result) # [[<Quantity(1.23, 'g / cm ** 3')>, <Quantity(25, 'degC')>]]
Decosta answered 26/3, 2022 at 22:29 Comment(0)
T
2

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

Tophus answered 1/3, 2013 at 15:25 Comment(1)
I see. I just gave a look at the source, and it seems to me that Ipython enters only when loading/unloading the extension, non in the body of the extension itself. But I guess that the pint wins, if not for the name and the pictureTophus

© 2022 - 2024 — McMap. All rights reserved.