如何在python中捕获“拆分”异常?

时间:2012-02-28 05:51:14

标签: python

我正在尝试解析电子邮件地址列表以删除用户名和“@”符号,只留下域名。

示例:blahblah@gmail.com 期望的输出:gmail.com

我用以下代码完成了这个:

for row in cr: 
    emailaddy = row[0]
    (emailuser, domain) = row[0].split('@')
    print domain

但问题是我遇到格式不正确的电子邮件地址。例如,如果该行包含“aaaaaaaaa”(而不是有效的电子邮件地址),则程序崩溃并显示错误

(emailuser, domain) = row[0].split('@')
ValueError: need more than 1 value to unpack. 

(正如您所期望的那样)我宁愿不更新抓取域名并转到下一条记录,而不是检查所有电子邮件地址的有效性。如何正确处理此错误并继续前进?

所以列出:

blahblah@gmail.com
mmymymy@hotmail.com
youououou
nonononon@yahoo.com

我希望输出为:

gmail.com
hotmail.com

yahoo.com

谢谢!

8 个答案:

答案 0 :(得分:13)

你想要这样的东西吗?

try:
    (emailuser, domain) = row[0].split('@')
except ValueError:
    continue

答案 1 :(得分:3)

您只需过滤掉不包含@的地址。

>>> [mail.split('@')[1] for mail in mylist if '@' in mail]
['gmail.com', 'hotmail.com', 'yahoo.com']
>>>

答案 2 :(得分:3)

怎么样?
splitaddr = row[0].split('@')
if len(splitaddr) == 2:
    domain = splitaddr[1]
else:
    domain = ''

这甚至会处理aaa@bbb@ccc之类的情况并使其无效('')。

答案 3 :(得分:2)

试试这个

In [28]: b = ['blahblah@gmail.com',
 'mmymymy@hotmail.com',
 'youououou',
 'nonononon@yahoo.com']

In [29]: [x.split('@')[1] for x in b if '@' in x]
Out[29]: ['gmail.com', 'hotmail.com', 'yahoo.com']

答案 4 :(得分:2)

这样做你想要的:

import re

l=["blahblah@gmail.com","mmymymy@hotmail.com",
   "youououou","nonononon@yahoo.com","amy@bong@youso.com"]

for e in l:
    if '@' in e:
       l2=e.split('@')
       print l2[-1]
    else:
       print

输出:

gmail.com
hotmail.com

yahoo.com
youso.com

它处理电子邮件可能包含多个“@”的情况,并且只接受相应的RH。

答案 5 :(得分:1)

if '@' in row[0]:
    user, domain = row[0].split('@')
    print domain

答案 6 :(得分:0)

我们可以将不带'@'符号的字符串视为一个简单的用户名:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: ems.erp.mmdu.com.forfirebasestorage, PID: 31905
java.lang.IllegalArgumentException: method ems.erp.mmdu.com.forfirebasestorage.Post.setBids argument 1 has type java.util.TreeMap, got java.util.HashMap
    at java.lang.reflect.Method.invoke(Native Method)
    at com.google.firebase.firestore.util.ApiUtil.invoke(com.google.firebase:firebase-firestore@@18.0.1:61)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.0.1:702)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.0.1:675)
    at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@18.0.1:504)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@18.0.1:243)
    at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@18.0.1:97)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:203)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:121)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:183)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:101)
    at ems.erp.mmdu.com.forfirebasestorage.AmmarFragment$1.onSuccess(AmmarFragment.java:105)
    at ems.erp.mmdu.com.forfirebasestorage.AmmarFragment$1.onSuccess(AmmarFragment.java:98)
    at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:200)
    at android.app.ActivityThread.main(ActivityThread.java:6956)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)

答案 7 :(得分:0)

也许最好的解决方案是同时避免异常处理。 您可以通过使用内置函数 partition() 来做到这一点。它类似于 split() 但在找不到分隔符时不会引发 ValueError 。
阅读更多:
https://docs.python.org/3/library/stdtypes.html#str.partition