I have a string : 5kg
.
I need to make the numerical and the textual parts apart. So, in this case, it should produce two parts : 5
and kg
.
For that I wrote a code:
grocery_uom = '5kg'
unit_weight, uom = grocery_uom.split('[a-zA-Z]+', 1)
print(unit_weight)
Getting this error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-66-23a4dd3345a6> in <module>()
1 grocery_uom = '5kg'
----> 2 unit_weight, uom = grocery_uom.split('[a-zA-Z]+', 1)
3 #print(unit_weight)
4
5
ValueError: not enough values to unpack (expected 2, got 1)
print(uom)
Edit: I wrote this:
unit_weight, uom = re.split('[a-zA-Z]+', grocery_uom, 1)
print(unit_weight)
print('-----')
print(uom)
Now I am getting this output:
5
-----
How to store the 2nd part of the string to a var?
Edit1: I wrote this which solved my purpose (Thanks to Peter Wood):
unit_weight = re.split('([a-zA-Z]+)', grocery_uom, 1)[0]
uom = re.split('([a-zA-Z]+)', grocery_uom, 1)[1]