needs:

needs to fill a large number of information in a JSON file into a fixed form of Excel form. It can also be completed by simply copying and paste, but wants to be lazy, so it helps to solve problems with Python.

Windows7 +Python2.7 +Xlwt

: specific analysis:

of the original documents for the JSON list, there are more than one dictionary list, generate Excel files need to be key button corresponding to the list of dictionary order, that is to say, all the "XX" key value corresponding to the write in one column, and each dictionary the keys of the key guarantee on the same line. The
solution is to read the JSON file, then traverse the keys and values of the dictionary, read the first dictionary and write Excel after the line, and read the second dictionaries.

Code:

 coding: UTF-8 # -*- -*- import xlwt import JSON # create excel sheet workbook = xlwt.Workbook (encoding='utf-8') worksheet = workbook.add_sheet ('sheet1') # set worksheet.write header (0, 0, label='NAME'(0) worksheet.write, 1, label='LEN') and worksheet.write (0, 2, 0, 3 (label='ID') worksheet.write label='OTHER', with open) # read JSON files ('test.json','r') as f: data = json.load (f) # JSON dictionary written excel # loop control variable is used to write the cell, feeling a better expression of val1 = 1, val2 = 1 val3 = 1 val4 = 1 for list_item in data: for key, value in list_item.items (if): key = = "NAME": worksheet.write (val1, 0, value) val1 = 1 elif key = = "LEN": worksheet.write (val2, 1, value)  val2 += 1   elif key == "ID":    worksheet.write(val3, 2, value)    val3 += 1   elif key == "OTHER":    worksheet.write(val4, 3, value)    val4 += 1   else:    pass  # 保存 workbook.save('OK.xls')  

辅助文件:

 test.json  [   {    "OTHER": "code",    "NAME": "whc",    "LEN": 100,    "ID": "01-0001"   },   {    "OTHER": "house",    "NAME": "ikd",    "LEN": 200,    "ID": "01-0002"   },   {    "OTHER": "thank",    "NAME": "qxf",    "LEN": 300,    "ID": "01-0003"   },   {    "OTHER": "music",    "NAME": "kmn",    "LEN": 400,    "ID": "01-0004"   },   {    "OTHER": "big",    "NAME": "vbf",    "LEN": 500,    "ID": "01-0005"   },   {    "OTHER": "over",    "NAME": "wsr",    "LEN": 600,    "ID": "01-0006"   } ]  

>

ok.xls

here to write the picture description

strong>

1 and other Description: < / code above directly generate sample alignment results and map, can be added in the code format control.
2, in the course of the actual use of the list dictionary also contains a dictionary, the same goes into the traversal.
3, the code is very simple, I hope you can give advice to help improve.

is the whole content of this article, I hope to help you, and hope that you can support a lot of scripting home.


This concludes the body part