我的Python 3程序有問題。我使用Mac OS X.此代碼運行正常。Python 3 - ValueError:沒有足夠的值來解壓縮(預計3,得到2)
# -*- 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('[email protected]', '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('[email protected]', 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('[email protected]', 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)
這意味着函數'unpaidMembers.items()'不返回元組形式的3項。試着打印它的值來知道你得到了什麼類型的返回值:'print unpaidMembers.items()' –
你的代碼不適合你的回溯。在代碼中,問題得到糾正,但缺少':'。 –
看起來你想要將包含兩個項目的元組拆分爲三個不同的項目。就像@CarlesMitjans所說的那樣,試着打印'unpaidMembers.items()'返回的值。您可能需要做一些額外的處理才能將它變成3個項目。 –