我正在嘗試使用 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>
uj5u.com熱心網友回復:
這是通過拆分字串并稍后將它們連接在一起來執行此操作的一種快速而骯臟的方法。
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>']
uj5u.com熱心網友回復:
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333184.html
