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

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.

主站蜘蛛池模板: 合作市| 林甸县| 牙克石市| 济源市| 内乡县| 麻城市| 中山市| 池州市| 鹿泉市| 申扎县| 色达县| 沂源县| 神农架林区| 金溪县| 民权县| 关岭| 河池市| 滨海县| 临沧市| 彰武县| 宁乡县| 会宁县| 威远县| 澜沧| 东宁县| 上杭县| 镇远县| 太谷县| 疏勒县| 房山区| 内丘县| 吕梁市| 东城区| 泰来县| 太湖县| 宁乡县| 苍山县| 湖北省| 建湖县| 喀喇| 文安县|