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'
Use str.split(sep) to parse the string str by the delimeter sep into a list of strings. Call str. split(sep, maxsplit) and state the maxsplit parameter to specify the maximum number of splits to perform. These splits are executed from the left hand side.
The length of a string is the number of characters where the width of a control is its size in pixel (per default). You may multiply number of character by a constant number of pixels however. string.
foo("string") passes a Python str object to a C function which will later assign the string to char *c_ptr .
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')>]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With