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

时间:2017-02-15 20:15:22

标签: python python-3.x

我的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

当我尝试将下一个值添加到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)

终端显示错误:

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)

5 个答案:

答案 0 :(得分:1)

在这一行:

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

unpaidMembers.items()每次迭代必须只有两个值。

这是一个用来说明问题的小例子:

这将有效:

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

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

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

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

答案 1 :(得分:1)

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

lastname = sheet.cell(row=r, column=3).value
什么东西;目前该程序只是忘了它

你可以这样做两行,就像这样

unpaidMembers[name] = lastname, email

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

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

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

答案 2 :(得分:0)

由于unpaidMembers字典,因此在使用.items() - (键,值)调用时,它始终返回两个值。您可能希望将数据保留为元组列表[(name, email, lastname), (name, email, lastname)..]

答案 3 :(得分:0)

ValueErrors:在Python中,值是存储在特定对象内的信息。在Python中遇到ValueError意味着您尝试将值分配给该对象的内容存在问题。

在您的案例名称,姓氏和电子邮件中有3个参数,但未付费会员仅包含其中2个。

  

姓名,姓氏,未付费的Members.items()中的电子邮件   所以你应该参考数据   或者您的代码可能是
  姓氏,使用unpaidMembers.items()发送电子邮件   要么   姓名,电子邮件地址为unpaidMembers.items()

答案 4 :(得分:0)

1。首先应该了解错误的含义

错误not enough values to unpack (expected 3, got 2)的意思是:

一个 2部分元组,但分配给 3个值

并且我已经编写了演示代码为您展示:


#!/usr/bin/python
# -*- coding: utf-8 -*-
# Function: Showing how to understand ValueError 'not enough values to unpack (expected 3, got 2)'
# Author: Crifan Li
# Update: 20191212

def notEnoughUnpack():
    """Showing how to understand python error `not enough values to unpack (expected 3, got 2)`"""
    # a dict, which single key's value is two part tuple
    valueIsTwoPartTupleDict = {
        "name1": ("lastname1", "email1"),
        "name2": ("lastname2", "email2"),
    }

    # Test case 1: got value from key
    gotLastname, gotEmail = valueIsTwoPartTupleDict["name1"] # OK
    print("gotLastname=%s, gotEmail=%s" % (gotLastname, gotEmail))
    # gotLastname, gotEmail, gotOtherSomeValue = valueIsTwoPartTupleDict["name1"] # -> ValueError not enough values to unpack (expected 3, got 2)

    # Test case 2: got from dict.items()
    for eachKey, eachValues in valueIsTwoPartTupleDict.items():
        print("eachKey=%s, eachValues=%s" % (eachKey, eachValues))
    # same as following:
    # Background knowledge: each of dict.items() return (key, values)
    # here above eachValues is a tuple of two parts
    for eachKey, (eachValuePart1, eachValuePart2) in valueIsTwoPartTupleDict.items():
        print("eachKey=%s, eachValuePart1=%s, eachValuePart2=%s" % (eachKey, eachValuePart1, eachValuePart2))
    # but following:
    for eachKey, (eachValuePart1, eachValuePart2, eachValuePart3) in valueIsTwoPartTupleDict.items(): # will -> ValueError not enough values to unpack (expected 3, got 2)
        pass

if __name__ == "__main__":
    notEnoughUnpack()

使用VSCode调试效果:

notEnoughUnpack CrifanLi

2。为您的代码

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

但是出错 ValueError: not enough values to unpack (expected 3, got 2)

表示unpaidMembers中的每个项目(一个元组值),只有1部分:email,与上面的代码相对应

    unpaidMembers[name] = email

因此应将代码更改为:

for name, email in unpaidMembers.items():

为避免错误。

但是显然您期望额外的lastname,因此应将上述代码更改为

    unpaidMembers[name] = (email, lastname)

更好地更改为更好的语法:

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

然后一切都很好,很清楚。