Clear a specific tag from a Python

Asked 2 years ago, Updated 2 years ago, 15 views

"<title>", "</title>" I want to erase these strings from the a string If you list it

['<title>i', 'love', 'you', 'so', 'much', 'baby</title>']```` 

How can I solve this?

a = "<title>i love you so much baby</title>"

a_list = a.split()

b = ["love"]

for idx, value in enumerate(a_list):
    if value in b:
        a_list[idx] = "_______"
        # print (if you see the word love)
        # # print(temp)
    elif value in a_list:
        a_list[idx] = value + " "
        # print ("If you can't see the word love")


re = " ".join(a_list)
print(re)

python

2022-09-21 10:00

2 Answers

>>> a = "<title>i love you so much baby</title>"
>>> a.replace("<title>", "").replace("</title>", "")
'i love you so much baby'


2022-09-21 10:00

import re
>>> a = "<title>i love you so much baby</title>"
>>> tag = 'title'
>>> print(re.findall('<%s>(.+)</%s>' % (tag, tag), a))
['i love you so much baby']

I've used regular expressions.


2022-09-21 10:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.