我想序列化一个平凡的对象(只属于/值), 我在我的课上有这种方法。python json序列化尝试/除了
def to_json(self,client):
service=self._service
path="jsonphp/"
path+=client
if not os.path.exists(path):
os.makedirs(path)
with cdd(path):
print os.getcwd()
path=service+".json"
with open(path, 'w') as outfile:
try:
json.dump(self.__dict__, outfile)
except:
print "serialization failed"
尝试之前的整个部分/除了要导航到正确的文件夹。它似乎工作顺利。但是当我使用这种方法时,它始终打印:serialization failed
从我的except
即使我的file.json被创建并且数据正确存储在其中:{"_attr1": "On", "_attr2": "On", "_attr3": "Off"}
我认为只有在发生错误时才访问except
块。
我的问题是下面的问题:是否是“正常”的行为try/except
?
如果不是,如何获得有关我除了捕捉的异常的信息?
难道它是在代码上的某处(在我呼吁to_json()
之前)在那里捕捉的豁免?
缩进问题? –
我不这么认为。你是指在这个函数调用之前的某个地方? – pwnsauce