python: extract float from a python list of string( AUD 31.99).
I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this:
['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
I need to get the float from the string item list so that i can later save it in a new list to get the total of them.
Desired output:
[31.40, 32.99, 37.24]
I have already tried these:
newList = re.findall("\d+\.\d+", tot[0])
print(newList)
Output:
[31.40]
But How can I use this for all the item elements?
I am new to python, this is just for some work i do, wanted to see the total using python instead of using excel`s find & replace option.
thanks
解决方案
You can use the map function:
inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)
Output:
[31.4, 32.99, 37.24]