I'm trying to read in a dictionary from a file, and extract its keys and values to print a string. When I try to print the current key and values, it says that there's too many values to unpack: expected 1 but got 2. I don't get why this would be occuring?
Input: {'CS307': ['Violet', 'Liam'], 'CS352': ['Amelia'], 'CS422': ['Finn', 'Violet']} Expected Output (as a string): Violet CS307 CS422 Liam CS307 Amelia CS352 Finn CS422 This is what I've got so far:
import ast def reverse(filename, students): newDict = {} listOfStudents = [] with open(filename, 'r') as myFile: content = myFile.read() for line in content: newDict = ast.literal_eval(content) for key, value in newDict: print("Key:", newDict[key]) listOfStudents.extend(newDict[key]) print("All Students:", listOfStudents) print(newDict) myFile.close() Line 10 @ for key, value is where I'm getting a Value error.
https://stackoverflow.com/questions/66057004/valueerror-when-reading-dictionary-from-txt-file February 05, 2021 at 11:05AM
没有评论:
发表评论