如何仅删除具有给定前提条件的某些字符

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>

我想要得到的是在不破坏 link 的情况下删除“-”。所以最后的结果一定是这样的:

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