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

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.

主站蜘蛛池模板: 海盐县| 佛坪县| 霍州市| 泰顺县| 兰坪| 古丈县| 海门市| 鹤山市| 新闻| 石阡县| 白水县| 达拉特旗| 天柱县| 武清区| 无为县| 华阴市| 青龙| 晋中市| 栾城县| 安国市| 泽普县| 盐边县| 金川县| 曲松县| 伽师县| 德惠市| 五大连池市| 兴业县| 广州市| 绥棱县| 台北县| 天等县| 临沂市| 三门县| 读书| 水城县| 永丰县| 彰化市| 巴东县| 逊克县| 白城市|