我想序列化一個平凡的對象(只屬於/值), 我在我的課上有這種方法。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