python 尋找數字

使用 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/

本篇發表於 未分類。將永久鍊結加入書籤。

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>