簡體   English   中英

如何從字符串列表中刪除不需要的字符?

[英]how to remove unwanted character from a list of strings?

只是想為我的場景找到合適的解決方案。 假設我有一個包含 2 個元素的列表,如下所示:

old_str = ['https://www[dot]exabeam[dot]com','https://www[dot]google[dot]com/#q=exa+beam']

我希望 output 為['www.exabeam.com', 'www.google.com']

我可以很好地使用以下邏輯將 '[dot]' 替換為 '.'

new_str= []
for new_char in old_str:
    replaced_str = new_char.replace('[dot]', '.')
    new_str.append(replaced_str)
print(new_str)

output 是:

[['https://www.exabeam.com'], ['https://www.google.com/#q=exa+beam']]

那么,如何實現預期的 output 呢? 請幫忙

謝謝

使用urllib

from urllib.parse import urlparse

[urlparse(i.replace("[dot]", ".")).netloc for i in old_str]

Output:

['www.exabeam.com', 'www.google.com']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM