繁体   English   中英

基于数字后面的空格分割线的Python方式

[英]Pythonic way to split a line based on spaces coming after number

line="Map: 1   Cumulative CPU: 3.83 sec   HDFS Read: 4598507 HDFS Write: 748757 SUCCESS"

我有这样一条线。 我想要以下曼纳的清单。

list=['Map: 1','Cumulative CPU: 3.83 sec','HDFS Read: 4598507','HDFS Write: 748757']

我对正则表达式不太满意,我认为可以实现obj的唯一方法是根据整数和浮点数后面的空格来分割此行。 有人可以帮我解决这个问题。 感谢高级。

您可以使用此正则表达式:

\S[^:]*: \d+(?:\.\d+ sec)?

用法:

re.findall(r'\S[^:]*: \d+(?:\.\d+ sec)?', line)

说明:

\S[^:]*        # look for a non-space character and match up to...
:              # the next colon
\d+            # followed by digits
(?:\.\d+ sec)? # and optionally some floating point digits and the string "sec"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM