In python, insert data in dictionary form into Excel and pythonexcel cyclically.
1. We can see the following data in dictionary form:
list=[["2891-1", "D"],["2892-1", "D"],["2896-1", "B"],["2913-1", 0],["2913-2", 1],["2913-3", 1]]
This list exists in the Database
2. We perform a data conversion for the word points of these styles.
Convert the list into a dictionary (view the code)
#-*-Coding: UTF-8-*-import pymysqlimport jsonimport refrom xlsxwriter import Workbookdatanew ={}# result data is the listfor item in result in the query database: datanew [item ['login _ name'] = list (eval (item ['ancer']) # create a new xlsx file (if the original file with the same name is overwritten) book = xlsxwriter. workbook ('C:/Users/Administrator/Desktop/xxx.xlsx') # create a new form with the default name "sheet1". Enter the character parameter to specify the name sheet1 = book. add_worksheet () # Dictionary data # cyclic dictionary key value num = [a for a in datanew] lennum = len (num) for a in range (lennum ): lena = len (datanew [num [a]); lena1 = datanew [num [a]; try: print (lena1) lena1.sort () handle T Exception as e: print (e) a1 = num [a] sheet1.write (a + 1, 0, a1) b1x = [] for B in range (lena): b1 = (lena1 [B]) [0] b2 = (lena1 [B]) [1] b1x. append (b1); sheet1.write (a + 1, B + 1, b2) if a = 0: for y in range (len (b1x )): bx = b1x [y] sheet1.write (0, y + 1, bx) book. close ()
Run the above code and we will get the format like this
Summary
The above section describes how to insert dictionary-type data into an Excel file in python. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!