[英]How to remove only certain characters with a pre condition given
我正在尝试使用 Python 从字符串列表中删除特定字符。
我的字符串是这样的:
<p><a href="first/Fruit-Shop-One.html">Fruit-Shop-One</a></p>
<p><a href="first/Fruit-Shop-Two.html">Fruit-Shop-Two</a></p>
我想要的是在不破坏链接的情况下删除“-”。 所以最终的结果一定是这样的:
<p><a href="first/Fruit-Shop-One.html">Fruit Shop One</a></p>
<p><a href="first/Fruit-Shop-Two.html">Fruit Shop Two</a></p>
这是通过拆分字符串并稍后将它们连接在一起来执行此操作的一种快速而肮脏的方法。
strings = ['<p><a href="first/Fruit-Shop-One.html">Fruit-Shop-One</a></p>', '<p><a href="first/Fruit-Shop-Two.html">Fruit-Shop-Two</a></p>']
for string in strings:
new_string = string.split('">')[0] + '">' + string.split('">')[1].replace("-", " ")
输出:
<p><a href="first/Fruit-Shop-One.html">Fruit Shop One</a></p>
<p><a href="first/Fruit-Shop-Two.html">Fruit Shop Two</a></p>
或者在列表理解中
new_strings = [string.split('">')[0] + '">' + string.split('">')[1].replace("-", " ") for string in strings]
输出:
['<p><a href="first/Fruit-Shop-One.html">Fruit Shop One</a></p>', '<p><a href="first/Fruit-Shop-Two.html">Fruit Shop Two</a></p>']
from bs4 import BeautifulSoup
string_one = '<p><a href="first/Fruit-Shop-One.html">Fruit-Shop-One</a></p>'
soup = BeautifulSoup(string_one, "html.parser")
for a in soup.findAll('a'):
a.string = a.string.replace('-', ' ')
new_string = str(soup)
print(soup)
# <p><a href="first/Fruit-Shop-One.html">Fruit Shop One</a></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.