gpt4 book ai didi

python - 如何在 python 中计算和打印 .txt 文件中的特定字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:26 24 4
gpt4 key购买 nike

我在收到此问题的输出时遇到了一些问题。基本上,我有一个文本文件( https://www.py4e.com/code3/mbox.txt ),我尝试首先让 python 打印其中找到了多少个电子邮件地址,然后在后续行中打印每个地址。我的输出示例如下所示:

Received: (from apache@localhost)

There were 22003 email addresses in mbox.txt
for source@collab.sakaiproject.org; Thu, 18 Oct 2007 11:31:49 -0400

There were 22004 email addresses in mbox.txt

X-Authentication-Warning: nakamura.uits.iupui.edu: apache set sender to zach.thomas@txstate.edu using -f

There were 22005 email addresses in mbox.txt

我在这里做错了什么?这是我的代码

fhand = open('mbox.txt')
count = 0
for line in fhand:
line = line.rstrip()
if '@' in line:
count = count + 1
print('There were', count, 'email addresses in mbox.txt')
if '@' in line:
print(line)

最佳答案

以下内容修改您的代码以使用正则表达式在文本行中查找电子邮件。

import re

# Pattern for email
# (see https://www.geeksforgeeks.org/extracting-email-addresses-using-regular-expressions-python/)

pattern = re.compile(r'\S+@\S+')

with open('mbox.txt') as fhand:
emails = []
for line in fhand:
# Detect all emails in line using regex pattern
found_emails = pattern.findall(line)
if found_emails:
emails.extend(found_emails)

print('There were', len(emails), 'email addresses in mbox.txt')
if emails:
print(*emails, sep="\n")

输出

There were 44018 email addresses in mbox.txt
stephen.marquard@uct.ac.za
<postmaster@collab.sakaiproject.org>
<200801051412.m05ECIaH010327@nakamura.uits.iupui.edu>
<source@collab.sakaiproject.org>;
<source@collab.sakaiproject.org>;
<source@collab.sakaiproject.org>;
apache@localhost)
source@collab.sakaiproject.org;
stephen.marquard@uct.ac.za
source@collab.sakaiproject.org
....
....
...etc...

关于python - 如何在 python 中计算和打印 .txt 文件中的特定字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58945106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com