简体   繁体   中英

Finding a date and replacing a word - Python

I have a code as below where i am filtering out dates which are older then 2 days and making it bold and changing it's color.

In this I also want to replace the Warning to where the date found is older then 2 days.

Can anyone please help how we can achieve it through below code.

import re
from datetime import datetime
ht ="""<tr>
<td>32356</td>
<td>cfra03</td>
<td>04-07-2020</td>
<td>Ssv</td>
<td>Warning</td>
</tr>
<tr>
<td>32367</td>
<td>c017</td>
<td>04-08-2020</td>
<td>SDR</td>
<td>Completed</td>
</tr>
<tr>
<td>29451</td>
<td>c10</td>
<td>04-05-2020</td>
<td>SR</td>
<td>Warning</td>
</tr>
"""

date_var = re.findall(r"[\d]{2}-[\d]{2}-[\d]{4}", ht)
for s in date_var:
    element_date = datetime.strptime(s, "%m-%d-%Y")
    if (datetime.now() - element_date).days > 2:
        ht = ht.replace(s, '<b><font color="#ff0000">'+s+'</font></b>')
print (ht)  

current output is-

<tr>
<td>32356</td>
<td>cfra03</td>
<td>04-07-2020</td>
<td>Ssv</td>
<td>Warning</td>
</tr>
<tr>
<td>32367</td>
<td>c017</td>
<td>04-08-2020</td>
<td>SDR</td>
<td>Completed</td>
</tr>
<tr>
<td>29451</td>
<td>c10</td>
<td><b><font color="#ff0000">04-05-2020</font></b></td>
<td>SR</td>
<td>Warning</td>
</tr>

Output should be-

<tr>
<td>32356</td>
<td>cfra03</td>
<td>04-07-2020</td>
<td>Ssv</td>
<td>Warning</td>
</tr>
<tr>
<td>32367</td>
<td>c017</td>
<td>04-08-2020</td>
<td>SDR</td>
<td>Completed</td>
</tr>
<tr>
<td>29451</td>
<td>c10</td>
<td><b><font color="#ff0000">04-05-2020</font></b></td>
<td>SR</td>
<td><b><font color="#ff0000">Failed</font></b></td>
</tr>
'''

This code:

import re
from datetime import datetime
ht ="""<tr>
<td>32356</td>
<td>cfra03</td>
<td>04-07-2020</td>
<td>Ssv</td>
<td>Warning</td>
</tr>
<tr>
<td>32367</td>
<td>c017</td>
<td>04-08-2020</td>
<td>SDR</td>
<td>Completed</td>
</tr>
<tr>
<td>29451</td>
<td>c10</td>
<td>04-05-2020</td>
<td>SR</td>
<td>Warning</td>
</tr>
"""

result = []
rows = ht.split('<tr>')
for row in rows :
    date_var = re.findall(r"[\d]{2}-[\d]{2}-[\d]{4}", row, re.DOTALL)
    for s in date_var:
        element_date = datetime.strptime(s, "%m-%d-%Y")
        if (datetime.now() - element_date).days > 2:
            row = row.replace(s, '<b><font color="#ff0000">'+s+'</font></b>')
            row = row.replace( 'Warning', 'Failed') # or whatever
    result.append(row)

print ('<tr>'.join(result))

gives this result:

<tr>
<td>32356</td>
<td>cfra03</td>
<td>04-07-2020</td>
<td>Ssv</td>
<td>Warning</td>
</tr>
<tr>
<td>32367</td>
<td>c017</td>
<td>04-08-2020</td>
<td>SDR</td>
<td>Completed</td>
</tr>
<tr>
<td>29451</td>
<td>c10</td>
<td><b><font color="#ff0000">04-05-2020</font></b></td>
<td>SR</td>
<td>Failed</td>
</tr>

You may add HTML around Failed as you like.

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