前段時間由於一個業務的需求須要解析一個HDF格式的文件。在這以前也不知道到底什麼是HDF文件。百度百科的解釋以下:python
HDF是用於存儲和分發科學數據的一種自我描述、多對象文件格式。HDF是由美國國家超級計算應用中心NCSA(全稱:National Center for Supercomputing Application)建立的,爲了知足各類領域研究需求而研製的一種能高效存儲和分發科學數據的新型數據格式。HDF能夠表示出科學數據存儲和分佈的許多必要條件。markdown
使用Python解析固然會用到第三方的包,以下:tcp
import math
import pandas as pd
import xlwt
第一個是用來作數學計算的math包主要處理數學相關的運算
。至於關於pandas
的介紹請點擊這裏。xlwt這個包是寫HDF文件的。函數
使用Python讀取HDF文件的代碼以下:ui
with closing(pd.HDFStore(HDF_FILR_URL)) as store:
df = store[date]
# index shoule be end -> region -> group
df.reset_index(inplace=True)
df.set_index(["end", "region", "group"], inplace=True)
df.sort_index(inplace=True)
其實這樣獲取到數據以後就是pandas
提供的函數,獲取本身須要的數據。url
slice_df = df.loc[dt]
rtt = slice_df.rtt.unstack(level=0) / 1000
cwnd = slice_df.cwnd.unstack(level=0)
total = slice_df.total.unstack(level=0)
rows = rtt.index.tolist()
columns = rtt.columns.tolist()
最後寫入Excel,代碼以下:spa
def writexcel(listname, name, time):
#將數據寫入Excel
saveurl = EXCEL_FILR_URL + '%s_%s_%s.xls' % (AVG_RTT, time, name)
excel_file = xlwt.Workbook()
table = excel_file.add_sheet('tcpinfo')
index_row = 0
for item in listname:
for item_key, item_value in item.items():
table.write(index_row, 0, str(item_key))
table.write(index_row, 1, str(item_value[1][0]))
table.write(index_row, 2, str(item_value[1][1]))
table.write(index_row, 3, str(item_value[0]).decode('utf-8'))
index_row += 1
excel_file.save(saveurl)
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。excel