2017-02-15 65 views
-2

我的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) 
+2

這意味着函數'unpaidMembers.items()'不返回元組形式的3項。試着打印它的值來知道你得到了什麼類型的返回值:'print unpaidMembers.items()' –

+1

你的代碼不適合你的回溯。在代碼中,問題得到糾正,但缺少':'。 –

+0

看起來你想要將包含兩個項目的元組拆分爲三個不同的項目。就像@CarlesMitjans所說的那樣,試着打印'unpaidMembers.items()'返回的值。您可能需要做一些額外的處理才能將它變成3個項目。 –

回答

0

你可能想分配lastname你在這裏

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

讀出的東西;目前該方案只是忘記它

你能做到這兩條線後,像這樣

unpaidMembers[name] = lastname, email 

你的程序仍然會崩潰,在同一個地方,因爲.items()仍然不會給你3元組,而是一些有這種結構:(name, (lastname, email))

好消息是,蟒蛇可以處理這個

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

等等

0

在這一行:

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?沒有,沒有足夠的價值觀,那就是問題所在。

0

由於unpaidMembers字典當使用.items()(鍵值)調用時,它始終返回兩個值。您可能希望將數據保存爲元組列表[(name, email, lastname), (name, email, lastname)..]

相關問題