我正在使用 python 并且有一串電子郵件地址,如下所示。
email_addr = '[email protected], [email protected], [email protected]'
上面的字串看起來不錯,但是有時我收到了其中包含空白電子郵件地址的資料。
例如
email_addr = ' , , [email protected], [email protected], , , ,[email protected]
我正在使用str.split(',')并檢查很多錯誤。想知道是否有更好的方法來做到這一點?
我期望的最終價值:
email_addr = ' , , [email protected], [email protected], , , ,[email protected]
到:
email_addr = '[email protected],[email protected],[email protected]'
uj5u.com熱心網友回復:
嘗試:
import re
email_addr = " , , [email protected], [email protected], , , ,[email protected]"
email_addr = email_addr.replace(" ", "").strip(",")
email_addr = re.sub(r",{2,}", ",", email_addr)
print(email_addr)
印刷:
[email protected],[email protected],[email protected]
uj5u.com熱心網友回復:
不需要正則運算式。用于.split(',')拆分為字串串列。
email_lst = email_addr.split(',')
然后用逗號加入,但過濾掉空白值
email_addr2 = ",".join(e.strip() for e in email_lst if e.strip())
# '[email protected],[email protected],[email protected]'
在 Python 3.8 中,您可以使用 walrus 運算子來避免呼叫.strip()兩次:
email_addr2 = ",".join(e for ee in email_lst if (e := ee.strip()))
uj5u.com熱心網友回復:
如果我們使用正則運算式,如何獲取匹配串列[^, ] 然后加入所有專案?
[^, ],表示除and之外的任何字符 ,并且 表示“1 或多個”
import re
email_addr = " , , [email protected], [email protected], , , ,[email protected]"
email_cleaned = ",".join(re.findall("[^, ] ", email_addr))
print(email_cleaned)
uj5u.com熱心網友回復:
我很想在您進行驗證時進行驗證,并依賴email.utils.parseaddr這將在一定程度上確保電子郵件客戶端會接受它們
>>> parse_email_addr("Foo Bar <[email protected]>")
('Foo Bar', '[email protected]')
from email.utils import parseaddr as parse_email_addr
email_addr = ' , , [email protected], [email protected], , , ,[email protected]'
result = ",".join(filter(None, (parse_email_addr(email)[1] for email in email_addr.split(","))))
# '[email protected],[email protected],[email protected]'
我也很想考慮壞欄位,這可能代表一些輸入錯誤(即。你是如何得到這些的?它們作為你程式的輸入是否正確?)
>>> result
'[email protected],[email protected],[email protected]'
>>> email_addr.rstrip(",").count(",") - result.count(",")
5
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/452320.html
下一篇:我將如何回傳此功能?
