Python methods for writing data to excel in a simple

Recently did a job needs to be written into the data processing Excel spreadsheet to save, so this brief introduction on how to use Python to save data to excel table.

Method requires that the data prior to introducing xlwt dependencies, installation is simple, direct pip install xlwt, if installed on the computer you do not need to repeat the installation.

Then we do a simple demo, adding three lines of data to excel in.

Specific code as follows:

 

# ! / Usr / bin / env Python 
# Coding = utf-8 
  
from xlwt Import *
 # need to support xlwt library 
# Import xlwt 
file = Workbook (encoding = ' utf-8 ' )
 # specify the file open format utf-8's 
file.add_sheet = Table ( ' Data ' )
 # specify open the file name 
  
Data = {
     " . 1 " : [ " Joe Smith " , 150,120,100 ],
     " 2 " : [ " John Doe " , 90,99,95 ],
    " 3 " : [ " Wang Wu " , 60,66,68 ] 
    } 
# dictionary data 
  
lData = [] 
num = [A for A in Data]
 # for loop num specified key value stored in the extraction 
num.sort ()
 # after removing the dictionary data without need to sort 
  
for X in NUM:
 # for loop data dictionary keys and values stored in batch in ldata 
  T = [int (X)]
   for a in data [X]: 
    T. the append (A) 
  ldata.append (T) 
  
for I, P in the enumerate (lData):
 #Writing data to files, i is the enumerate () function returns a serial number 
  for J, Q in the enumerate (P):
     # Print I, J, Q 
    table.write (I, J, Q) 
File.Save ( ' Data. XLSX ' )

 

 

 

Guess you like

Origin www.cnblogs.com/zhaoyingjie/p/11465126.html