首页 > 解决方案 > Count number of email addresses present in a file - Python

问题描述

Count total number of valid email addresses in a file.

sample file Input:

Hello from me
@gmail.com
how are you doing?
abc@gmail.com will meet xyz@yahoo.com pq@yahoo.co.in about the meeting 
sharp @2PM.okay see you  yes@yahoo.co.in there.

Expected output: 4

I have tried using this code. but it is returning empty list.

import re
with open("/Downloads/email.txt",'r') as e_add:
    for lst in e_add:
        lst = re.findall(r'[\w\.-]+@[\w\.-]+',lst)
        print(lst)


output:

[]
[]
[]
['abc@gmail.com', 'xyz@yahoo.com', 'pq@yahoo.co.in']
['yes@yahoo.co.in']

标签: pythonpython-3.x

解决方案


OP 的代码逐行运行,因此空列表表示在该行上没有找到有效的电子邮件。可以通过将整个文件读入字符串变量并对其执行正则表达式来解决此问题,或者计算每个列表的长度并更新一些计数器。

计数器方式

import re

n_email_addresses = 0
with open("/Downloads/email.txt",'r') as f
    for line in f:
        emails_found = re.findall(r'[\w\.-]+@[\w\.-]+', line)
        n_email_addresses += len(emails_found)
print(n_email_addresses)

读取整个文件方法

from pathlib import Path
import re

text = Path("/Downloads/email.txt").read_text()
emails = re.findall(r'[\w\.-]+@[\w\.-]+', text)
n_email_addresses = len(emails)
print(n_email_addresses)

推荐阅读