1. 程式人生 > 其它 >使用pandas對excel資料做塊狀提取

使用pandas對excel資料做塊狀提取

技術標籤:pythonpandaspythonexcelpandas

使用pandas對excel資料做塊狀提取

1、程式碼:

# coding=utf-8
'''
Function : excel to hrl
Author : wgn
Created time : 2020-8-13
Other :python=2.7.11,pandas=0.24.2
'''
import xlrd
import pandas as pd
import sys
#panda塊需要
reload(sys)
sys.setdefaultencoding('utf-8')

#開啟檔案,獲取excel檔案的workbook(工作簿)物件
workbook=xlrd.open_workbook("test.xlsx") #檔案路徑hrl/XXX.xls names=workbook.sheet_names() print(names) #以列表形式,輸出所有的表名 print(len(names))#備註:需要去重的話用:set1=set(name) for i in range(1, len(names)): # 加【1,】跳過第一個sheet頁【summary】 # 使用pandas模組讀取資料, 預設頭行為列表名,去掉【,header=None】不要第一行資料 df = pd.read_excel(
'test.xlsx', sheet_name=names[i]) print('\n'+names[i]+'.hrl'+'開始寫入...') # 寫入井號分隔sep# df.to_csv('./hrl/'+names[i]+'.hrl', header=None, sep='#', index=False) # 根據文件需求,插入3行資料頭 with open('./hrl/'+names[i]+'.hrl','r+') as file: content = file.read() file.seek(0,0) file
.write('#head;hrl;2.0;utf8\n'+ '#ref#speechfile#speaker#gender#reference word sequence#topic#;slotname#;slotvalue\n'+ 'head\n'+content) print(names[i]+'.hrl'+'完成!')

2、執行結果:
在這裡插入圖片描述