[英]Python: Extract value as it appears in Excel cell not underlying number
[英]Extract number from Excel cell in Python
我有許多 excel 單元格,每個單元格內都有數字和字母字符,例如: + 0.01 PD, OR, - 0.0005 PD, OR, - 0.001 PD
,...。我怎樣才能從單元格中提取數字? 謝謝
以下是如何使用re.findall()
,假設單元格中的所有數字都是浮點數:
import re
s = '+ 0.01 PD, OR, - 0.0005 PD, OR, - 0.001 PD'
print(re.findall('\d+\.\d+',s))
Output:
['0.01', '0.0005', '0.001']
如果偶然也會有整數,請添加'|'
到正則表達式字符串充當or
運算符:
print(re.findall('\d+\.\d+|\d+',s))
請注意,所有'\d'
也可以替換為'[0-9]'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.