python 資料存成Excel

 

在資料整理完成後,經常會將資料存成EXCEL檔,這時候會用到一個相當好用的套件叫做 openpyxl 使用起來相當簡單,下面就請各位看官看看把python 資料存成Excel檔有多麼簡單

 

# 首先需要import openpyxl這個套件

import openpyxl

from openpyxl import Workbook

 

# python建立一個Excel空白活頁簿

excel_file = Workbook()

# 建立一個工作中表

sheet = excel_file.active

 

# 先填入第一列的欄位名稱

sheet['A1'] = 'columnA'

sheet['B1'] = 'columnB'

sheet['C1'] = 'columnC'

sheet['D1'] = 'columnD'

 

# 使用迴圈逐列增加,這邊為了讓大家看清楚才用2開始跑,我們實際上是用append的方式往下新增資料,所以從0開始讀取資料即可

i = 2

while i < 10:

    columnA = 'A'+str(i)

    columnB = 'B'+str(i)

    columnC = 'C'+str(i)

    columnD = 'D'+str(i)

#     實際將資料寫入每一列

    sheet.append([columnA, columnB, columnC, columnD])

    i = i + 1

 

# 儲存成XLSX

excel_file.save('sample.xlsx')

 

以上就是使用python將資料存成Excel檔案的方式

 

如果覺得對你有幫助的話請幫小弟按個讚吧~

 

 

Python相關文章:

 

python import 路徑說明

Python使用matplotlib畫折線圖(Line chart)

python使用matplotlib畫圓餅圖(Pie chart)

pandas Dataframe常用的資料處理方法-(合併資料、選擇欄位、刪除欄位、刪除列)

 

arrow
arrow

    newaurora 發表在 痞客邦 留言(0) 人氣()