Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to align the properties of print objects in Python

Shulou Source: shulou.com Published: 2022-06-02 10:27:40 10月28日 Update

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.

Tags: Object output attribute special code method processing help support normal clear ugly one search one line content dictionary character string that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology OPPO Reno Huawei Xiaomi