Python 正则表达式应用
列表存在下列数据,如何运用正则表达式或其他办法提取成对应格式? 从左边提取到右边。
2.50s(+/-10%) ['2.5','10','%']
14s(+/-10%) ['1.4','10','%']
2.75Wh(+/-10%) ['2.75','10','%']
22.5cm3 (+/-10%) ['22.5','10','%']
320 0C(+/-5) ['320','5']
315 0C(+/-5) ['315','5']
# 这样试试
import re
ls=["2.50s(+/-10%)",
"14s(+/-10%)",
"2.75Wh(+/-10%)",
"22.5cm3 (+/-10%)",
"320 0C(+/-5)",
"315 0C(+/-5)"]
pat = r"([\d\.]+).*-(\d+)(%)?"
fori in ls :
lst=re.findall(pat, i,re.I)
l=[ k for k in list(lst) if k!='']
print(l)
页:
[1]