2017-02-15 5 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ループに次の値を追加しようとしているときに始まります。この行で

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

2つのアイテムを含むタプルを3つの異なるアイテムに解凍しようとしているようです。 @ CarlsMitjansのように、 'unpaidMembers.items()'が返す値を出力してみてください。 3つの項目にするには、追加の処理が必要な場合があります。 –

答えて

0

はおそらく、あなたが何かにここに

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

読み出すされlastnameを割り当てます。現在のプログラムはそれを忘れ

あなたは何ができる

ような後の二行、そう

unpaidMembers[name] = lastname, email 

.items()はまだあなたに3つのタプルを与えることはありませんので、まだ、同じ場所でクラッシュしますあなたのプログラムではなく、このような構造を持っているものは:良いニュースがある(name, (lastname, email))

、Pythonは扱うことができ、この

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

など

0

# 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) 

ターミナルはエラーを示し

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

unpaidMembers.items()が反復ごとに2つだけの値を有していなければなりません。

これは動作します:ここで

は、問題を説明するための小さな一例である

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() - (key、value)で呼び出すと常に2つの値が返されます。あなたはタプル[(name, email, lastname), (name, email, lastname)..]のリストとしてあなたのデータを保持したいかもしれません。

関連する問題