Convert the list of JSON descriptions into HTML table classes (I think the encoding style is much better)

Source: Internet
Author: User

 

 1   #  ! /Usr/bin/ENV Python  2   #  -*-Coding: UTF-8 -*-  3   4   Import  JSON  5   6   7 Src_table = {  8   " Quick account for decoration  " :{ "  Transtype  " : "  Transaction type  "  ,  9               "  Transdate  " : "  Transaction date  "  , 10               "  Currency  " : "  Transaction currency  "  ,  11               "  Inacct  " : U "  Capital Inflow account  "  ,  12               " Inamount  " : "  Inbound amount  "  ,  13               "  Outacct  " : "  Fund outflow account  "  ,  14               "  Outamount " : "  Outbound amount  "  ,  15               "  Memo  " : "  Remarks  "  ,  16               "  Category  " :"  Income and expenditure items  "  ,  17               "  Createtime  " : "  Creation Time  "  18   },  19   }  20   21   Class Json2tablebuilder:  22       #  -----------------------------------  23       Def   _ Init __  (Self, jsondata, Src ):  24 Self. _ jsondata = Jsondata  25 Self. _ rowlist = [Self. _ getdescriptionrow (SRC)]  26 Self. _ rowlist = self. _ rowlist +Self. _ converttolist ()  27   28       #  -----------------------------------  29       Def  _ Getdescriptionrow (self, Src ):  30 Row = None  31           If  Src_table.has_key (SRC ):  32 Row = Src_table [SRC] 33               Return  Row  34           Else  :  35               Raise Exception ( "  Error: Source Error  "  )  36           37       #  -----------------------------------  38      Def  Outputhtml (Self ):  39 Index = 0  40 Html = ""  41 Count = Len (self. _ rowlist)  42           While Index < Count:  43 HTML + = Self. genrowhtml (INDEX)  44 Index = index + 1 45           Return   '  <Table class = "qbj_json_table"> \ n  ' + HTML + '  </Table> \ n  '  46   47       #  -----------------------------------  48       Def  Genrowhtml (self, index ): 49 Row = Self. _ rowlist [Index]  50 Cellvalues = Row. Values ()  51 S = ""   52           For Value In  Cellvalues:  53 S + = "  <TD>  " + Self. _ sprintf (value) +"  <TD>  "  54           Return   "  <Tr>  " + S + "  <Tr> \ n  "  55   56       #  -----------------------------------  57       ''' Convert JSON data into an object and check  '''  58       Def  _ Converttolist (Self ):  59 Data = JSON. Loads (self. _ jsondata)  60           If Type (data) Not   In  (List, dict ):  61               Raise Exception ( " Error: input error  "  )  62   63           If Type (data) Is  Dict:  64 Data = [Data]  65           Return  Data  66       67       ''' Convert other types to string  '''  68       Def  _ Sprintf (self, field ):  69           If Type (field) In  (INT, float ):  70 Field = STR (field)  71   72           If Type (field) Is UNICODE:  73 Field = field. encode ( '  Utf8  '  )  74   75           If Len (field) = 0:  76 Field = "  & Nbsp;  "  77   78          Return  Field  79   80   '''  Test entry  '''  81   If   _ Name __ = "  _ Main __  "  :  82 S = '''  83  [  84   {"Transtype": "income", "transdate": "2011-11-09", "currency": "RMB ",  85   "Inacct": "Current Account", "inamount": 1000.0, "outacct": "", "outamount": 0, "memo": "wage income in March ",  86   "Category": "working", "createtime": "03:02:34. 000 "},  87   {"Transtype": "income", "transdate": "2011-12-09", "currency": "RMB ",  88   "Inacct": "cash", "inamount": 1000.0, "outacct": "", "outamount": 0, "memo": "wage income in March ", 89   "Category": "working", "createtime": "2011-12-09 13:22:01. 000 "}  90   ]  91       '''  92   93 Builder = json2tablebuilder (S, "  Quick account for decoration  "  )  94 S2 = Builder. outputhtml ()  95      Print S2

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.