使用 re 模組從字串中提取數字
Python 的 re 模組還提供了可以搜尋字串並提取結果的函式。re 模組提供了 findall() 方法,該方法返回所有匹配結果的列表。下面給出一個示例程式碼。
import re
temp_string = "Hi my age is 32 years and 250.5 days12″
print(temp_string)
print([float(s) for s in re.findall(r'-?\d+\.?\d*', temp_string)])
https://www.delftstack.com/zh-tw/howto/python/python-extract-number-from-string/