python - 在 python 2.7 中使用 smtp 时出现 keyerror

标签 python python-2.7 dictionary keyerror

for row in csv_reader:
    username = row ["Username"]
    pwd = row ["Password"]
    email = row["Username"]
    grades = row["Grades"]
    comments = row["Comments"]

**我的错误是:用户名=行[“用户名”]

KeyError:'用户名'**

最佳答案

我认为名为 row 的值可能不包含 Username 键。 您可以通过 print(row)print(csv_reader) 查看所有 key 。

确保 csv_reader 是包含字典和 Username 键的列表。 它看起来像这样:

csv_reader = [{"Username": "McCree", "Password": "Secret", "Grades": "100" ...}]

关于python - 在 python 2.7 中使用 smtp 时出现 keyerror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465893/

相关文章:

python - 定位没有 id 或 class 属性的表

Python:如何使用opencv选择边框之间的图像部分

Python,非阻塞线程

python-2.7 - 如何使用xlwings在Excel中获取公式结果

python - 一起添加字典

python - 如何在 Iron Python 脚本中加载可移植的 .NET 库?

python - 多个模块,一个类的单个实例 - Python

c# - 'order respecting' 字典的解决方案

python - Blob : Printing Track Information

python - 基于模型的KeyProperty实例值的NDB查询