python - ValueError: dictionary update sequence element #0 has length 1; 2 is required while reading from file -
i'm trying read dictionary off file , make string dictionary. have this,
with open("../resources/enemystats.txt", "r") stats: line in stats: if self.kind in line: line = line.replace(self.kind + " ", "") line = dict(line) return line and line in txt file is,
slime {'hp':5,'speed':1} i'd able return dict can access hp , other values enemy character.
dict() not parse python dictionary literal syntax; won't take string , interpret contents. can take dictionary or sequence of key-value pairs, line doesn't match criteria.
you'll need use ast.literal_eval() function instead here:
from ast import literal_eval if line.startswith(self.kind): line = line[len(self.kind) + 1:] return literal_eval(line) i've adjusted detection of self.kind little; i'm assuming wanted match if self.kind found @ start of line.
Comments
Post a Comment