我正在使用带有Exchangelib库的python中的一些代码,我正在尝试通过计数器打印特定电子邮件地址的列表,该计数器计算从该电子邮件地址接收的电子邮件数量。这是我的代码,但没有任何打印输出。
from exchangelib import Credentials, Account
from collections import defaultdict
emails = ['email1@example.com','email2@example.dk']
credentials = Credentials('random@user.dk', 'Brute')
account = Account('random@user.dk', credentials=credentials, autodiscover=True)
def count_senders(emails):
counts = defaultdict(int)
for email in emails:
counts[email.sender.email_address] += 1
return counts
print(counts)
答案 0 :(得分:1)
我在代码中看不到您的函数调用。也许是问题所在?
count_senders(emails)
另一个问题是您无法在返回之后进行打印。像这样更改功能顺序:
def count_senders(emails):
counts = defaultdict(int)
for email in emails:
counts[item.sender.email_address] += 1
print(counts)
return counts
更新: 尝试在运行代码之前放入真实的电子邮件和凭据。
emails = ['email1@example.com','email2@example.dk']
for email in emails:
credentials = Credentials(email, 'Brute')
account = Account(email, credentials=credentials, autodiscover=True)
counts = defaultdict(int)
for item in account.inbox.all().order_by('-datetime_received')[:100]:
print(item.subject, item.sender, item.datetime_received)
counts[email.sender.email_address] += 1
print(counts)