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

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.

主站蜘蛛池模板: 桂阳县| 自治县| 五峰| 雅江县| 扎鲁特旗| 高阳县| 枞阳县| 通城县| 德保县| 岫岩| 靖宇县| 黄浦区| 左云县| 庆云县| 宿迁市| 花莲县| 黑河市| 蛟河市| 竹山县| 云梦县| 潼关县| 诸暨市| 阿鲁科尔沁旗| 监利县| 顺义区| 泽库县| 临桂县| 泰州市| 敦煌市| 罗山县| 朔州市| 平乡县| 越西县| 扶余县| 芜湖县| 揭东县| 壶关县| 通河县| 广汉市| 乡宁县| 资溪县|