python - 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']
解决方案
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)
推荐阅读
- php - Woocommerce - 在结帐期间更改总文本并添加费用
- swiftui - 导致导航链接初始化丢失的状态变量
- redis - 通过 RedissonScript 在 redis 上执行 lua 脚本时的解码器问题
- python - 在熊猫中,如何计算每列与系列的协方差?
- serenity-bdd - SerenityJS - NPM CI 命令入门问题 - Serenity-BDD CLI jar
- python - WebdriverWait 不起作用,还有其他选择吗?(蟒蛇硒)
- analytics - 来自 Runbook 的 Kusto 查询
- node.js - 多个 Heroku Dynos 上的数据库迁移
- sql - 两个表的累积余额
- javascript - 如何引用由 ECS/Fargate 的容器洞察创建的 CloudWatch 指标