python json序列化try / except(python json serialization try/except)

编程入门 行业动态 更新时间:2024-10-28 18:22:38
python json序列化try / except(python json serialization try/except)

我正在尝试序列化一个简单的对象(只有attributs / values),我在课堂上有这个方法。

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"

try / except之前的整个部分是导航到正确的文件夹。 它似乎运作顺利。 但是当我使用这个方法时,它总是打印: serialization failed , except我创建了file.json并且数据正确存储在其中: {"_attr1": "On", "_attr2": "On", "_attr3": "Off"}我认为只有在发生错误时才会访问except块。

我的问题是休息:是try/except “正常”的行为吗?

如果没有,如何获取有关我的除外捕获的异常的信息?

它可能是代码中某个上方的一个exeption(在我调用to_json() )被to_json()吗?

I'm trying to serialize a trivial object (only attributs / values), I have this methode in my class.

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"

the whole part before the try/except is to naviguate to the correct folder. It seems to work smoothly. But when I use this methode, it always print : serialization failed from my except even tho my file.json is created and the data are correctly stored in it : {"_attr1": "On", "_attr2": "On", "_attr3": "Off"} I thought the except block was only visited if an error had occured.

My questions are the fallowing : is that the "normal" behaviour of try/except ?

If not, how to have information about the exception that my except is catching ?

Could it be an exeption from somewhere upper in the code (before I call to_json()) that is catched there ?

最满意答案

我发现这更像是一个异常处理问题而不是其他任何问题。 由于您可能在编程工作中遇到异常,因此正确处理它们非常重要。 很少应该除非如此宽泛,如果您不理解为什么会有例外,您应该打印例外。 这将使您能够搜索异常并确定问题的根本原因,并在您继续学习Python时对您有所帮助。

将您的除外改为:

except Exception as e: print "serialization failed", e

这将在您的控制台上打印确切的异常,并允许您查找或寻求帮助。

I find this to be more of an exception handling question than anything else. Since you're likely to have exceptions throughout your programming endeavors, it's important to handle them properly. Rarely should the except be so broad, and in the case where you don't understand why there is an exception, you should be printing the exception. This will give you the ability to search for the exception and determine what the root cause of your problem is and will help you greatly as you continue to learn Python.

Change your except to read:

except Exception as e: print "serialization failed", e

This will print the exact exception to your console and allow you to look up, or ask for help.

更多推荐

本文发布于:2023-07-05 14:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   python   json   serialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!