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

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.

主站蜘蛛池模板: 长子县| 杭锦后旗| 西畴县| 博野县| 三原县| 枝江市| 丹棱县| 四川省| 南涧| 松桃| 榆社县| 运城市| 高清| 巍山| 永善县| 湟中县| 军事| 安达市| 依安县| 大田县| 昌乐县| 车致| 稷山县| 威宁| 赞皇县| 闻喜县| 饶阳县| 威信县| 互助| 那曲县| 盐源县| 石渠县| 凤城市| 印江| 上饶市| 稷山县| 类乌齐县| 囊谦县| 华宁县| 罗城| 临泽县|