【问题标题】:I don't understand why I get a "too many values to unpack" error我不明白为什么会出现“太多值无法解包”错误
【发布时间】:2020-04-25 03:37:49
【问题描述】:

当我运行它时,我可以让它询问我的密码,但是当它启动我的“for”循环时,我在重新分级我的数据时遇到错误。

import csv, smtplib, ssl
import pandas as pd 


newhire = pd.read_csv('New_Hires_Test.csv', delimiter = ",", skiprows=7)   #Open up the document and 
skip header

newhire["Email"] = "test@gmail.com"     #Add a row 'Email' and add email address

mask = newhire["New IT Item"] == "New"      #brings back only new in "New IT Item column"

message = """\Subject: {Effective Date}
From: test@gmail.com
To: test@gmail.com 

Hello IT, 

We have a new user by the name of {StartDate}. The new user will be starting on {Employee} and will 
be working at {PC}. 
Their title will be {Title} and their supervisor is {Supervisor}"""

from_address = "test@gmail.com"
password = input("Type your password and press enter: ")


context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(from_address, password)

    for Employee, StartDate, PC, Title, Supervisor, Email in newhire[mask]:
        server.sendmail(
            from_address,
            Email,
            message.format(Employee=Employee, StartDate=StartDate, PC=PC, Title=Title, Email=Email, 
Supervisor=Supervisor)
        )

print('Emails were sent successfully!')

***ValueError: too many values to unpack (expected 6)***

我正在查看我的“for”循环,我看到它有 6 列我正在寻找。我的数据都是字符串,我设置了一个分隔符只是为了确保所有数据都正确分隔。我认为我要么忽略了问题,要么不理解我的数据。

****New_Hires_Test.csv***
Report:  ,,,All HR Action Requests: IT CAFs - New Hires (KB 
Copy),,,,,,,,,,,,,
Sorted By:  ,,,Effective Date Descending,,,,,,,,,,,,,
Filtered By:  ,,,Initiator Filter: All Employees; Employee Filter: All 
Employees; Approver Filter: All Employees; HR Action = Hire Employee (Staff) 
- Step 2,,,,,,,,,,,,,
Date & Time:  ,,,01/06/2020 09:12p,,,,,,,,,,,,,
Generated By:  ,,,Haywood,,,,,,,,,,,,,
Company:  ,,,Corp Solutions(6122261),,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
New IT Item,StartDate,Effective Date,Employee Name,PC,Title,Supervisor 
Name,Manager 2 Name,O365,ID 
Badge,Computer,Avionte,GP,Hotspot,Tablet,TimeZone,HR Action
New,01/13/2020,02/03/2020,Elizabeth Anne Everts,Internal Staff/003 - 
003,Onboarding Specialist,Suzanne Mitchell,Angela 
Robbins,Yes,,No,Yes,No,No,No,Eastern,Hire Employee (Staff) - Step 2
New,01/13/2020,01/13/2020,Karla Ramirez,Internal Staff/204 - 003, 
Recruiter,Scott Clark,Shane Houser,Yes,,Standard 
Laptop,Yes,Yes,No,No,Central,Hire Employee (Staff) - Step 2
New,01/13/2020,01/06/2020,Megan Arreola,Internal Staff/221 - 
003,Recruiter,Elsa Muneton,Amanda Stewart,Yes,,No,Yes,No,No,No,Eastern,Hire 
Employee (Staff) - Step 2

【问题讨论】:

  • 请分享一个数据框的小例子
  • 请剪切/粘贴整个回溯。只是ValueError 没用。
  • 在解包元组之前尝试将循环变量保存为临时变量,然后打印该临时值,例如for x in newhire[mask]: print x。错误前的最后一行将是损坏的行。
  • 看看newhire[mask]的内容。它不是你想的那样。 for Employee, StartDate, PC, Title, Supervisor, Email in newhire[mask] 正在抛出错误,因为 newhire 中没有六个值。
  • 我在上面添加了我的 csv 文件示例

标签: python pandas csv ssl smtplib


【解决方案1】:

iterrows() 调用替换你的 for 循环,如下所示:

        for i, r in newhire[mask].iterrows():
            server.sendmail(
                from_address,
                r["Email"],
                message.format(Employee=r["Employee"],
                   StartDate=r["StartDate"],
                               PC=r["PC"],
                               Title=r["Title"],
                               Email=r["Email"], 
                               Supervisor=r["Supervisor"]
                )
            )

EDIT1:

newhire[mask] in for a,....,c in newhire[mask] 将解压缩 newhire[mask] int a,....,c 的内容(整个数据框)。您要做的是仅解压缩一行的内容。为此,您可以使用newhire[mask].iterrows(),它在每一行都返回其索引和行实例本身。

您也可以使用itertuples(),如本问题Pandas for loop over dataframe gives too many values to unpack中所述

【讨论】:

  • 这行得通,但为什么行得通。我想了解为什么事情会奏效。
  • 更新为 EDIT1 供您参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2018-03-15
  • 1970-01-01
相关资源
最近更新 更多