Python 记录一个坑
我有一个项目,批量登录网页账号的,在每登录一个账号的时候,可能会出现验证备用邮箱的字符串,把这个字符串提取出来后与原本完整的邮箱进行比对,这里会出现一个大坑。有时候python之禅的“极简”还是得放放。
下面是提取的字符串,类似:
svi•••••••@vi•.••.com svi•••••••@fo•••••.com 28••••••@qq.com svi•••••••@ou•••••.com vip••••••@ou•••••.com liu••••••@live.com svi•••••••@hotmail.com ann••••••••••@ou•••••.com
把上面的字符串存储后,第二步是要对这些字符串进行还原:
1 if '28' in strResult: 2 strResult = '2810xx11@qq.com' 3 elif 'svi' and '@fo' in strResult: 4 strResult = 'svipxxon@foxmail.com' 5 elif 'svi' and '@vi' in strResult: 6 strResult = 'svipdraxx@vip.qq.com' 7 elif 'vip' and '@ou' in strResult: 8 strResult = 'vipdxxx@outlook.com' 9 elif 'svi' and '@ou' in strResult: 10 strResult = 'svipdxxx@outlook.com' 11 elif 'liu' and '@live' in strResult: 12 strResult = 'liuxxxg@live.com' 13 elif 'ann' and '@ou' in strResult: 14 strResult = 'annxxx.xan@outlook.com' 15 elif 'svi' and '@ho' in strResult: 16 strResult = 'svipxxx@hotmail.com' 17 18 print(strResult)
上面的代码就会出现坑,有些运行的效果都不是预期的,匹配出来的让人头晕脑胀,特别是三个outlook.com的邮箱,一会结果是svipdxxx@outlook.com,一会结果是vipdxxx@outlook.com,更有甚者想匹配svi和vip的时候会出现结果annxxx.xan@outlook.com。搞得我头晕了,卡了一天,然后请教了一个朋友,大神,没有两分钟,就跟我说:“代码看上去没错,换种写法试试,elif 'svi' in strResult and '@vi' in strResult”。就是这句话,醍醐灌顶,恍然大悟,所以我就有点怪python之禅了。我以为我写这个条件的时候写得已经很死了,他说了之后,我才发现,在完成预期的前提下,他这样算是写的最死的了。不说了,将代码贴上,记录一下:
1 strResult = 'vip••••••@ou•••••.com' 2 3 if '28' in strResult: 4 strResult = '2810xx11@qq.com' 5 elif 'svi' in strResult and '@fo' in strResult: 6 strResult = 'svipxxon@foxmail.com' 7 elif 'svi' in strResult and '@vi' in strResult: 8 strResult = 'svipdraxx@vip.qq.com' 9 elif 'vip' in strResult and '@ou' in strResult: 10 strResult = 'vipdxxx@outlook.com' 11 elif 'svi' in strResult and '@ou' in strResult: 12 strResult = 'svipdxxx@outlook.com' 13 elif 'liu' in strResult and '@live' in strResult: 14 strResult = 'liuxxxg@live.com' 15 elif 'ann' in strResult and '@ou' in strResult: 16 strResult = 'annxxx.xan@outlook.com' 17 elif 'svi' in strResult and '@ho' in strResult: 18 strResult = 'svipxxx@hotmail.com' 19 20 print(strResult)
改完后,代码运行如预期。