我正在开发一个使用NetworkX图的程序,在两次运行之间,使用jsonpickle将其存储在文本文件中。解开文件时,有时会出现以下错误: self.graph = jsonpickle.decode(graph_json) 文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,第41行,在解码中 返回context.restore(data,reset = reset,classes = classes) 还原中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,第150行 值= self._restore(obj) _restore中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,第207行 返回restore(obj) _restore_object中的文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,第323行 返回self._restore_object_instance(obj,cls) 文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,行390,在_restore_object_instance中 返回self._restore_object_instance_variables(obj,instance) _restore_object_instance_variables中的第440行,文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,第440行 实例= self._restore_from_dict(obj,实例) 文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jsonpickle/unpickler.py”,第423行,位于_restore_from_dict setattr(instance,k,value) AttributeError:无法设置属性
这是我的代码:
self.graph = nx.Graph()
file = open('network.txt','w')
graph_json = jsonpickle.encode(self.graph)
file.write(graph_json)
file.close()
file = open('network.txt', 'r')
graph_json = file.read()
file.close()
self.graph = jsonpickle.decode(graph_json)
最后一行引发错误。