In python, insert data in dictionary form into Excel and pythonexcel cyclically.

Source: Internet
Author: User

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!

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.