python list of dictionaries will be exported as an Excel file method

The dictionary lists the following content export to Excel spreadsheet file format:

The figure written on the dictionary list, please refer to the article: https://blog.csdn.net/weixin_39082390/article/details/97373951

python list of dictionaries will be exported to Excel file, as follows:

1, the official Excel installed python libraries ------ xlwt

Can be installed directly in the terminal: pip install xlwt

After installation, the introduction in the program library xlwt

import xlwt

2 The list of dictionaries exported to excel file:

Import xlwt
 Import PANDAS AS PD 


DEF export_excel (Export):
    # The dictionary list into DataFrame 
   PF = pd.DataFrame (List (Export))
    # specifies the field order 
   Order = [ ' ROAD_NAME ' , ' bus_plate ' , ' Timeline ' , ' road_type ' , ' Site ' ] 
   PF = PF [Order]
    # column names replaced Chinese 
   columns_map = {
       ' ROAD_NAME ' : 'Route ' ,
       ' bus_plate ' : ' license ' ,
       ' Timeline ' : ' Time ' ,
       ' road_type ' : ' direction ' ,
       ' Site ' : ' Site ' 
   } 
   pf.rename (Columns = columns_map, InPlace = True)
    # Specify generation the Excel spreadsheet name 
   file_path = pd.ExcelWriter ( ' name.xlsx ')
    # Replace empty cells 
   pf.fillna ('  ' , InPlace = True)
    # output 
   pf.to_excel (file_path, encoding = ' UTF-8 ' , index = False)
    # save the table 
   file_path.save ()
 IF  __name__ == ' __main__ ' :
     # will analyze the complete list of export to excel spreadsheet 
    export_excel (tables)

3, Export to Excel spreadsheet:

 

Guess you like

Origin www.cnblogs.com/zhengxt-520/p/11446121.html