我被高中python的作业困住了,而且我的时间很短,无法完成。我非常感谢完整且有效的(语法正确的)文章来比较和编辑我的代码。
作业内容为: 编写一个程序,该程序打开并读取mbox.txt,查找包含电子邮件地址的行,计算找到的电子邮件地址的数量,然后一次仅使用一行将所有这些地址打印到输出文件中。
可以在此处找到指向mbox.txt的链接:https://www.py4e.com/code3/mbox.txt
一吨!
import re
name = input("Enter file:")
if len(name) < 1 : name = "mbox.txt"
handle = open(name)
email_matches = []
found_emails = []
final_emails = []
counts = dict()
for lines in handle :
# look for specific characters in document text
if lines.find('@') : continue
# increments the count variable for each match I found
lines.split()
# appends the required lines to the matches list
email_matches.append(lines)
for email in email_matches :
out = email
found = re.findall(r'[\w\.-]+@[\w\.-]+', out)
found_emails.append(found)
for item in found_emails :
count = item[0]
final_emails.append(count)
for items in final_emails:
counts[items] = counts.get(items,0) + 1
maximum = max(counts, key = lambda x: counts.get(x))
print (maximum, counts[maximum])
答案 0 :(得分:0)
我已经修改了您的代码,虽然很脏,但似乎可行。仅供参考。
import re
# name = input("Enter file:")
# if len(name) < 1 : name = "mbox.txt"
email_matches = []
found_emails = []
# read mbox.txt file
with open("mbox.txt", 'r') as f:
for line in f.readlines() :
line = line.strip()
# look for specific characters in document text
if '@' in line :
# increments the count variable for each match I found
sentence_split_by_space = line.split()
for one_sentence in sentence_split_by_space:
# in case more than one email in one line
if '@' in one_sentence:
# appends the required lines to the matches list
email_matches.append(one_sentence)
# print(email_matches)
for email in email_matches :
if re.match(r'[\w\.-]+@[\w\.-]+', email):
found_emails.append(email)
print(email)
print("Total counts: ", len(found_emails))
# write results to txt file
with open('mbox_count_emails.txt', 'w') as f:
f.write('Total counts: ' + str(len(found_emails)) + '\n')
for one_email in found_emails:
f.write(one_email + '\n')