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

如何计算及;在python中打印.txt文件中的特定字符串?,python,Python,我在这个问题上收到的输出有点问题。基本上,我有一个文本文件(),我试图首先让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

我在这个问题上收到的输出有点问题。基本上,我有一个文本文件(),我试图首先让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)


你能更清楚地说明你的预期产出与实际产出相比是什么吗

您有两个
if'@'行
语句应该组合在一起;没有理由问同样的问题两次

计算包含
@
符号的行数,然后每行打印当前计数

如果只想打印一次计数,则将其放在for循环的外部(后面)

如果您想要打印电子邮件地址,而不是打印包含它们的整行,那么您需要进行更多的字符串处理,以从该行中提取电子邮件


完成文件后别忘了关闭它。

下面修改代码,使用正则表达式查找文本行中的电子邮件

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...
mbox.txt中有44018个电子邮件地址
斯蒂芬。marquard@uct.ac.za
;
;
;
apache@localhost)
source@collab.sakaiproject.org;
斯蒂芬。marquard@uct.ac.za
source@collab.sakaiproject.org
....
....
等

对不起,我的目标是输出:“mbox.txt中有_uu电子邮件地址”,然后在后续行中打印每个电子邮件地址,以更新您的问题以说明这一点。目前,它说的是“我的全部输出”,但我认为这不是你的全部输出。我相信我的答案符合你的要求。谢谢你,我已经更新了。我把两个打印行都放在循环之外,但现在我只得到输出中的地址量。你还说我需要做更多的字符串处理来从行中提取电子邮件。这是什么样子的?解决这类问题的一种方法是将其分解,一次只处理一件事情(帮助你集中注意力)。因此,与其让整个循环工作,不如让行处理为一行工作,比如:
line=“Received:(fromapache@localhost)“
然后尽你所能让它发挥作用。请注意,仅仅因为一行有一个
@
,并不意味着它是一个有效的电子邮件地址。您输出的示例不是有效的公共电子邮件。非常感谢。Darryl u aG@AlexHaley--很高兴我能帮忙。