在python中分裂线

时间:2015-01-10 03:30:53

标签: python text split

text = "                            ABCD-10630Re: Alert: abc.server.com/kafka stauts Status,ABCD-10629Re: Alert: db01.server.com/Replication lag,ABCD-10601Re: Alert: web-app.server.com/apache service down check,ABCD-10571Re: Alert: slave01.server.com/Replication lag,
"

我从卷曲输出和HTML标签的清理中获得了上述文本。我希望能够从文本的其余部分拆分票证(参见下面的示例)并使用python单独打印出来。

示例:

ABCD-1063O           Re: Alert: abc.server.com/kafka stauts Status,
ABCD-10629           Re: Alert: db01.server.com/Replication lag,
.
.
.

请帮忙。

3 个答案:

答案 0 :(得分:1)

 split_list = text.split(',')

 for i in range(len(split_list) - 1):
     re_index = split_list[i].index('Re')
     print "{0}      {1}".format(split_list[i][0:re_index].strip(), split_list[i]
     [re_index:].strip())

答案 1 :(得分:0)

您可以使用re.findall

>>> text = "                            ABCD-10630Re: Alert: abc.server.com/kafka stauts Status,ABCD-10629Re: Alert: db01.server.com/Replication lag,ABCD-10601Re: Alert: web-app.server.com/apache service down check,ABCD-10571Re: Alert: slave01.server.com/Replication lag,   "
>>> re.findall(r'([A-Z]+-\d+)(Re[^,]+,)', text)
[('ABCD-10630', 'Re: Alert: abc.server.com/kafka stauts Status,'), ('ABCD-10629', 'Re: Alert: db01.server.com/Replication lag,'), ('ABCD-10601', 'Re: Alert: web-app.server.com/apache service down check,'), ('ABCD-10571', 'Re: Alert: slave01.server.com/Replication lag,')]
>>> for (x,y) in re.findall(r'([A-Z]+-\d+)(Re[^,]+,)', string):
        print(x+"\t"+y)


ABCD-10630  Re: Alert: abc.server.com/kafka stauts Status,
ABCD-10629  Re: Alert: db01.server.com/Replication lag,
ABCD-10601  Re: Alert: web-app.server.com/apache service down check,
ABCD-10571  Re: Alert: slave01.server.com/Replication lag,

答案 2 :(得分:0)

它不太可靠,但它可能符合您的目的:

 mylist = text.split(',')
 for i in mylist:
     print (i);