Pythonic方法根据数字后面的空格分割一条线

时间:2017-03-05 16:37:53

标签: python list split

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

我有这样一条线。我想要一个关注mannner的列表。

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

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

1 个答案:

答案 0 :(得分:2)

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

\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"