將如下的字典列表內容導出為Excel表格文件形式:
關於上圖字典列表的寫入,請參考文章:https://blog.csdn.net/weixin_39082390/article/details/97373951
python將字典列表導出為Excel文件的方法,如下所示:
1、安裝python官方Excel庫------xlwt
直接在終端進行安裝即可:pip install xlwt
安裝完成后,在程序中引入xlwt的庫
import xlwt
2將字典列表導出到excel文件中:
import xlwt import pandas as pd def export_excel(export): #將字典列表轉換為DataFrame pf = pd.DataFrame(list(export)) #指定字段順序 order = ['road_name','bus_plate','timeline','road_type','site'] pf = pf[order] #將列名替換為中文 columns_map = { 'road_name':'路線', 'bus_plate':'車牌', 'timeline':'時間', 'road_type':'方向', 'site':'站點' } pf.rename(columns = columns_map,inplace = True) #指定生成的Excel表格名稱 file_path = pd.ExcelWriter('name.xlsx') #替換空單元格 pf.fillna(' ',inplace = True) #輸出 pf.to_excel(file_path,encoding = 'utf-8',index = False) #保存表格 file_path.save() if __name__ == '__main__': #將分析完成的列表導出為excel表格 export_excel(tables)
3、導出為Excel表格: