In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python in how to align print object properties, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Align plot object attribut
The print output object dict is all huddled together in one line, which is ugly. So you want to output something similar to json alignment.
The basic idea is to convert to json format and then output. Do a random search and find the following code, which is available under normal circumstances:
Def obj_to_json (): stu = Student (28, 'male',' 1300000000000) print (type (stu)) # print (stu) stu = stu.__dict__ # convert the object to the dict dictionary print (type (stu)) # print (stu) j = json.dumps (obj=stu, indent=4) print (j)
However, if some objects contain some special properties, such as another object, an error will be reported using this method:
TypeError: Object of type xxx is not JSON serializable
In fact, JSON does not support such objects. We can customize the method for handling special objects. The complete code is as follows:
# encoding:utf-8# author: overimport jsonfrom datetime import datetime, dateclass Student (object): def _ _ init__ (self, age, sex, mobile, date): self.age = age self.sex = sex self.mobile = mobile self.date = date# extended json unresolvable type class ComplexEncoder (json.JSONEncoder): def default (self, obj): if isinstance (obj) Datetime): # return obj.strftime ('% Y-%m-%d% HGV% MVR% S') return str (obj) elif isinstance (obj, date): # return obj.strftime ('% Ymuri% mmure% d') return str (obj) else: try: return json.JSONEncoder.default (self) Obj) except Exception as e: print ('type not support:' + str (obj)) # default can not handle direct forcible string return str (obj) # json alignment output all attributes of the object Easy to view def printJson (obj): if hasattr (obj,'__dict__'): obj = obj.__dict__ # ensure_ascii=False Chinese does not become percentile # indent shrinks to increase the number of spaces j = json.dumps (obj, cls=ComplexEncoder, ensure_ascii=False, indent=4) print (j) if _ _ name__ = ='_ _ main__': stu = Student (28, 'male',' 130000000000' Datetime.now () printJson (stu)
Output:
{
"age": 28
"sex": "male"
"mobile": "13000000000"
"date": "2019-07-07 14 4315 51.466416"
}
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
I hope you will read it carefully.
© 2024 shulou.com SLNews company. All rights reserved.