简体   繁体   中英

Splitting lines in python

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

I get the above text from a curl output and some cleaning up of HTML tags. I want to be able to split the tickets(see the below example) from the rest of the text and print them out separately using python.

Example:

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

please help.

 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())

You could use 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,

It is not very reliable but it might fit your purpose:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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