python - 提取字符串中特定列下的值

标签 python string python-3.x

我的字符串如下所示

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/

相关文章:

python-3.x - python中的RTMP流监控

python - 如何计算选定范围的平均值?(pandas)

python - 如何从函数中的 pytest fixture 获取返回值,这样我就不需要使用附加参数调用该函数?

c# - 如何查找可调整大小/滚动文本框中显示的子字符串的索引?

c++ - 无符号字符串构造不可能

c++ - 时间戳转换错误

python - 字典中元组第一项的平均值

php - 使用 PHP 调用 Virtualenv’ed Python 脚本

python - 打印字典 (JSON) 人类可读

python - 如何使用 Python 从 telnet 响应中读取一行?