python - 提取字符串中特定列下的值
问题描述
我的字符串如下
Module Available Used Remaining
(Watts) (Watts) (Watts)
------ --------- -------- ---------
1 485.0 0.0 485.0
我需要在Used
. 我试图拆分字符串。有没有更好的方法来获得价值?
解决方案
使用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 - 动画表情符号?
- javascript - 在表格中使用带有角材料的表格
- amazon-web-services - AWS SES 仍然使用来自@us-west-2.amazonses.com
- mysql - 创建存储过程时mysql语法错误
- excel - 我将 Excel 与 VBA 一起使用,它间歇性地引发运行时错误 1004
- angular7 - 角度是否需要像 NGRX、NGXS、Akita 这样的 3rd 方状态管理?
- oracle - 您知道为什么当我尝试连接到 Oracle 服务器时会出现以下错误“状态:失败 - [LDAP:错误代码 32 - 无此类对象]”吗?
- ios - GroupHeaderTemplate 在 Xamarin.Forms iOS 项目中有额外的高度
- sqlite - SQLite3 将两个整数的结果相除
- sql - 数据模型指导,数据库选择用于更改过滤条件的聚合