Python 3 - ValueError:没有足够的值来解包(期望3,得到2)

时间:2023-01-25 18:18:02

I have a problem with my Python 3 program. I use Mac OS X. This code is running properly.

我的Python 3程序有问题。我使用Mac OS x,这段代码运行正常。

# -*- coding: utf-8 -*-
#! python3
# sendDuesReminders.py - Sends emails based on payment status in spreadsheet.

import openpyxl, smtplib, sys


# Open the spreadsheet and get the latest dues status.
wb = openpyxl.load_workbook('duesRecords.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')

lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value

# Check each member's payment status.
unpaidMembers = {}
for r in range(2, sheet.max_row + 1):
payment = sheet.cell(row=r, column=lastCol).value
if payment != 'zaplacone':
    name = sheet.cell(row=r, column=2).value
    lastname = sheet.cell(row=r, column=3).value
    email = sheet.cell(row=r, column=4).value
    unpaidMembers[name] = email


# Log in to email account.
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtpObj.ehlo()
smtpObj.login('abc@abc.com', '1234')


# Send out reminder emails.
for name, email in unpaidMembers.items()
body = "Subject: %s - przypomnienie o platnosci raty za treningi GIT Parkour. " \
       "\n\nPrzypominamy o uregulowaniu wplaty za uczestnictwo: %s w treningach GIT Parkour w ." \
       "\n\nRecords show  that you have not paid dues for %s. Please make " \
       "this payment as soon as possible."%(latestMonth, name, latestMonth)
print('Sending email to %s...' % email)
sendmailStatus = smtpObj.sendmail('abc@abc.com', email, body)

if sendmailStatus != {}:
    print('There was a problem sending email to %s: %s' % (email,
    sendmailStatus))
smtpObj.quit()enter code here

Problems starts when I am trying to add next value to the for loop.

当我试图为for循环添加下一个值时,问题就开始了。

# Send out reminder emails.
for name, lastname, email in unpaidMembers.items()
body = "Subject: %s - przypomnienie o platnosci raty za treningi GIT Parkour. " \
       "\n\nPrzypominamy o uregulowaniu wplaty za uczestnictwo: %s %s w treningach GIT Parkour w ." \
       "\n\nRecords show  that you have not paid dues for %s. Please make " \
       "this payment as soon as possible."%(latestMonth, name, lastname, latestMonth)
print('Sending email to %s...' % email)
sendmailStatus = smtpObj.sendmail('abc@abc.com', email, body)

Terminal shows error:

终端显示错误:

Traceback (most recent call last):
    File "sendDuesEmailReminder.py", line 44, in <module>
        for name, email, lastname in unpaidMembers.items():
ValueError: not enough values to unpack (expected 3, got 2)

3 个解决方案

#1


0  

You probably want to assign the lastname you are reading out here

您可能想要分配您正在阅读的lastname。

lastname = sheet.cell(row=r, column=3).value

to something; currently the program just forgets it

的东西;目前这个程序只是忘记了它。

you could do that two lines after, like so

你可以像这样做两行。

unpaidMembers[name] = lastname, email

your program will still crash at the same place, because .items() still won't give you 3-tuples but rather something that has this structure: (name, (lastname, email))

您的程序仍然会在相同的地方崩溃,因为.items()仍然不会给您3元组,而是具有这个结构的东西:(名称,(lastname, email))

good news is, python can handle this

好消息是,python可以处理这个问题。

for name, (lastname, email) in unpaidMembers.items():

etc.

等。

#2


0  

In this line:

在这条线:

for name, email, lastname in unpaidMembers.items():

unpaidMembers.items() must have only two values per iteration.

项目()每个迭代必须只有两个值。

Here is a small example to illustrate the problem:

这里有一个小例子来说明这个问题:

This will work:

这将工作:

for alpha, beta, delta in [("first", "second", "third")]:
    print("alpha:", alpha, "beta:", beta, "delta:", delta)

This will fail, and is what your code does:

这将会失败,这是您的代码所做的:

for alpha, beta, delta in [("first", "second")]:
    print("alpha:", alpha, "beta:", beta, "delta:", delta)

In this last example, what value in the list is assigned to delta? Nothing, There aren't enough values, and that is the problem.

在最后一个例子中,列表中的什么值被分配给了delta?没有什么,没有足够的价值,这就是问题所在。

#3


0  

Since unpaidMembers is a dictionary it always returns two values when called with .items() - (key, value). You may want to keep your data as a list of tuples [(name, email, lastname), (name, email, lastname)..].

由于unpaidMembers是一个字典,所以当使用.items()—(key, value)时,它总是返回两个值。您可能希望将您的数据保存为tuple(名称、电子邮件、lastname)的列表(名称、电子邮件、lastname)。

#1


0  

You probably want to assign the lastname you are reading out here

您可能想要分配您正在阅读的lastname。

lastname = sheet.cell(row=r, column=3).value

to something; currently the program just forgets it

的东西;目前这个程序只是忘记了它。

you could do that two lines after, like so

你可以像这样做两行。

unpaidMembers[name] = lastname, email

your program will still crash at the same place, because .items() still won't give you 3-tuples but rather something that has this structure: (name, (lastname, email))

您的程序仍然会在相同的地方崩溃,因为.items()仍然不会给您3元组,而是具有这个结构的东西:(名称,(lastname, email))

good news is, python can handle this

好消息是,python可以处理这个问题。

for name, (lastname, email) in unpaidMembers.items():

etc.

等。

#2


0  

In this line:

在这条线:

for name, email, lastname in unpaidMembers.items():

unpaidMembers.items() must have only two values per iteration.

项目()每个迭代必须只有两个值。

Here is a small example to illustrate the problem:

这里有一个小例子来说明这个问题:

This will work:

这将工作:

for alpha, beta, delta in [("first", "second", "third")]:
    print("alpha:", alpha, "beta:", beta, "delta:", delta)

This will fail, and is what your code does:

这将会失败,这是您的代码所做的:

for alpha, beta, delta in [("first", "second")]:
    print("alpha:", alpha, "beta:", beta, "delta:", delta)

In this last example, what value in the list is assigned to delta? Nothing, There aren't enough values, and that is the problem.

在最后一个例子中,列表中的什么值被分配给了delta?没有什么,没有足够的价值,这就是问题所在。

#3


0  

Since unpaidMembers is a dictionary it always returns two values when called with .items() - (key, value). You may want to keep your data as a list of tuples [(name, email, lastname), (name, email, lastname)..].

由于unpaidMembers是一个字典,所以当使用.items()—(key, value)时,它总是返回两个值。您可能希望将您的数据保存为tuple(名称、电子邮件、lastname)的列表(名称、电子邮件、lastname)。