【发布时间】:2015-02-10 15:09:40
【问题描述】:
我需要根据给定的文件创建字典。名为 student_data.txt 的文件内容如下:
Joy - 100
Rita - 90
John - 80
Sheena - 100
Smith - 100
Karl - 90
Andrew - 100
Klara - 100
Sarah - 90
Amy - 80
Sandy - 100
我想创建一个字典,使用学生的姓名作为键,分数作为值。
这是我尝试过的:
myDictionary = {}
myFile = open("student_data.txt", 'r')
for line in myFile:
key, value = line.strip().split('-')
myDictionary[key.strip()] = value.strip()
当我编译并运行它时,我收到一条错误消息“ValueError: Needs more than 1 value to unpack.”
【问题讨论】:
标签: python-3.x for-loop dictionary split strip