How Python automatically formats JSON files _python

Source: Internet
Author: User

The examples in this article describe how Python automatically formats JSON files. Share to everyone for your reference. Specifically as follows:

Here is the main implementation of the code confusing JSON file format.
And a little heap of Python's Common algorithm code

Full instance code click here to download the site.

Class Jsonformatter:def __init__ (self,intend=4,name= ""): Self.name=name self.intend=intend self.stack=[] Self.obj=none Self.source=self.get_source (name) Self.prepare () @staticmethod def json_str (s): Return ' "  ' +s+ ' ' @staticmethod def get_source (name): With open (name, ' R ') as F:return '. Join (F.read (). Split ()) def
  Prepare (self): Try:self.obj=eval (Self.source) except:raise Exception (' Invalid json string! ') def line_intend (self,level=0): return ' \ n ' + ' *self.intend*level def parse_dict (self,obj=none,intend_level=0): s Elf.stack.append (Self.line_intend (intend_level) + ' {') intend_level+=1 for Key,value in Obj.items (): Key=self.
      JSON_STR (str (key)) Self.stack.append (Self.line_intend (intend_level) +key+ ': ') self.parse (value,intend_level) Self.stack.append (', ') self.stack.append (Self.line_intend (INTEND_LEVEL-1) + '} ') def parse_list (Self,obj=none,inte nd_level=0): Self.stack.Append (Self.line_intend (intend_level) + ' [') intend_level+=1 for item in Obj:self.parse (Item,intend_level) Self.stack.append (', ') self.stack.append (Self.line_intend (INTEND_LEVEL-1) + '] "Def parse (self,obj,intend_level=0 ): If obj is None:self.stack.append (' null ') elif the obj is True:self.stack.append (' True ') elif obj
    Is False:self.stack.append (' False ') elif isinstance (obj, (int,long,float)): Self.stack.append (str (obj)) Elif isinstance (OBJ,STR): Self.stack.append (Self.json_str (obj)) elif isinstance (obj, (list,tuple)): Self. Parse_list (Obj,intend_level) elif isinstance (obj,dict): Self.parse_dict (obj,intend_level) else:raise
    Exception (' Invalid json type%s! '% obj) def render (self): Self.parse (self.obj,0) res_file= ' good ' +self.name Res= '. Join (Self.stack) with open (Res_file, ' W ') as F:f.write (res) Print res if __name__== "__main__": jf=j Sonformatter (name= "JSON").TXT ") Jf.render () 

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.