Python parsing hdf file

Source: Internet
Author: User

A period of time ago because a business needs to parse a HDF format file. Before that, I don't know what the HDF file is. The explanation of Baidu Encyclopedia is as follows:

HDF is a self-describing, multi-object file format for storing and distributing scientific data. HDF is a new data format for efficient storage and distribution of scientific data, created by the National Centre for Supercomputing Applications NCSA (full name: Nation Center for supercomputing application) in order to meet research needs in a variety of fields. HDF can represent many necessary conditions for the storage and distribution of scientific data.

Using Python parsing will of course use a third-party package, as follows:

import mathimport pandas as pdimport xlwt

The first one is for mathematical calculations math包主要处理数学相关的运算 . For pandas An introduction please click here. XLWT This package is written in HDF file.

The code for reading the HDF file using Python is as follows:

    withas 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)

In fact, after getting to the data is pandas the function provided to get the data you need.

    slice_df = df.loc[dt]    rtt = slice_df.rtt.unstack(level=01000    cwnd = slice_df.cwnd.unstack(level=0)    total = slice_df.total.unstack(level=0)    rows = rtt.index.tolist()    columns = rtt.columns.tolist()

Finally, write to Excel with the following code:

 def writexcel(listname, name, time):    #将数据写入ExcelSaveurl = Excel_filr_url +'%s_%s_%s.xls '% (Avg_rtt, time, name) Excel_file = XLWT. Workbook () Table = Excel_file.add_sheet (' Tcpinfo ') Index_row =0     forIteminchListName: forItem_key, Item_valueinchItem.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 + =1Excel_file.save (Saveurl)

Python parsing hdf file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.