官术网_书友最值得收藏!

Using HDF5 with PyTables

Let's first create an HDF5 file from the numeric data we have in the temp.csv file with the following steps:

  1. Get the numeric data:
import numpy as np
arr = np.loadtxt('temp.csv', skiprows=1, usecols=(2,3), delimiter=',')

  1. Open the HDF5 file:
import tables
h5filename = 'pytable_demo.hdf5'
with tables.open_file(h5filename,mode='w') as h5file:
  1.  Get the root node:
    root = h5file.root
  1. Create a group with create_group() or a dataset with create_array(), and repeat this until all the data is stored:
    h5file.create_array(root,'global_power',arr)
  1. Close the file:
    h5file.close()

Let's read the file and print the dataset to make sure it is properly written:

with tables.open_file(h5filename,mode='r') as h5file:
root = h5file.root
for node in h5file.root:
ds = node.read()
print(type(ds),ds.shape)
print(ds)

We get the NumPy array back.

主站蜘蛛池模板: 错那县| 赣州市| 任丘市| 新兴县| 吐鲁番市| 白城市| 罗源县| 鄂尔多斯市| 华池县| 滨州市| 绥江县| 马山县| 永康市| 镶黄旗| 阿拉善右旗| 偃师市| 汕头市| 竹山县| 乡宁县| 泾源县| 白河县| 东源县| 信宜市| 凤凰县| 河间市| 玉屏| 钟祥市| 保山市| 密山市| 东方市| 元朗区| 红安县| 绥德县| 大石桥市| 临高县| 酉阳| 门头沟区| 利川市| 平武县| 阿拉善左旗| 芦山县|