我的字符串如下所示
Module Available Used Remaining
(Watts) (Watts) (Watts)
------ --------- -------- ---------
1 485.0 0.0 485.0
我需要提取Used
下的值。我试图把绳子分开。有没有更好的方法来获取值(value)?
最佳答案
使用str.split
演示:
s = """Module Available Used Remaining
(Watts) (Watts) (Watts)
------ --------- -------- ---------
1 485.0 0.0 485.0"""
for i in s.split("\n"):
if i.strip()[0].isdigit():
val = i.split()
print(val[2])
使用正则表达式:
import re
val = re.findall("\d+\s+\d*\s+\d*\.\d*\s+\d*\.\d*\s+\d*\.\d*", s)
for v in val:
print(v.split()[2])
输出:
0.0
关于python - 提取字符串中特定列下的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201139/