你是对的,这个问题与 5 个元素的列表有关。
我能够复制这个问题。在我的例子中,列表有 9 个元素,但 read_hdf 函数要求每个表格单元格只有一个值。
下面是我使用 Pandas 编写的 Python 代码。不幸的是,我无法解决这个问题。
通过使用 h5py 库,我能够成功地前进。再往下是我的带有 h5py 库的 Python 代码。
熊猫
工作示例
import pandas as pd
test_output = pd.read_hdf('./nug_46.h5', '/NASTRAN/RESULT/NODAL/DISPLACEMENT')
print(test_output)
# returns
# ID X Y Z RX RY RZ DOMAIN_ID
# 0 3 -0.000561 -0.001269 0.001303 0.0 0.0 0.0 2
# 1 5 -0.001269 -0.000561 0.001303 0.0 0.0 0.0 2
# 2 6 -0.001342 -0.000668 0.001181 0.0 0.0 0.0 2
# 3 7 -0.001342 -0.000794 0.001162 0.0 0.0 0.0 2
# 4 8 -0.001335 -0.000893 0.001120 0.0 0.0 0.0 2
# ... ... ... ... ... ... ... ... ...
# 4878 20475 0.000000 0.000000 0.000000 0.0 0.0 0.0 2
# 4879 20478 0.000000 0.000000 0.000000 0.0 0.0 0.0 2
# 4880 100001 0.000000 0.000000 0.000000 0.0 0.0 0.0 2
# 4881 100002 0.000000 0.000000 0.000000 0.0 0.0 0.0 2
# 4882 100003 0.000000 0.000000 0.000000 0.0 0.0 0.0 2
非工作示例
test_output = pd.read_hdf('./nug_46.h5', 'NASTRAN/RESULT/ELEMENTAL/STRESS/HEXA')
print(test_output)
# returns an error
# Traceback (most recent call last):
# File "/home/apricot/PycharmProjects/python_hdf5_reader/venv/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 1654, in create_block_manager_from_blocks
# make_block(values=blocks[0], placement=slice(0, len(axes[0])))
# File "/home/apricot/PycharmProjects/python_hdf5_reader/venv/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 3041, in make_block
# return klass(values, ndim=ndim, placement=placement)
# File "/home/apricot/PycharmProjects/python_hdf5_reader/venv/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 125, in __init__
# f"Wrong number of items passed {len(self.values)}, "
# ValueError: Wrong number of items passed 9, placement implies 1
H5PY
工作示例
import h5py
file = h5py.File('./nug_46.h5', 'r')
# Open the dataset of compound type
dataset = file['/NASTRAN/RESULT/ELEMENTAL/STRESS/HEXA']
# Print the column names
column_names = dataset.dtype.names
print(column_names)
# returns
# ('EID', 'CID', 'CTYPE', 'NODEF', 'GRID', 'X', 'Y', 'Z', 'TXY', 'TYZ', 'TZX', 'DOMAIN_ID')
# Print the first ten rows of the dataset
# If you want to print the whole dataset, leave out the brackets and
# colon, e.g. enumerate(dataset)
for i, line in enumerate(dataset[0:10]):
print(line)
# returns
# (447, 0, b'GRID', 8, [ 0, 5, 6, 12, 11, 1716, 1340, 1346, 1345], ..., 2)
# (448, 0, b'GRID', 8, [ 0, 6, 7, 13, 12, 1340, 1341, 1347, 1346], ..., 2)
# (449, 0, b'GRID', 8, [ 0, 7, 8, 14, 13, 1341, 1342, 1348, 1347], ..., 2)
# (450, 0, b'GRID', 8, [ 0, 8, 9, 15, 14, 1342, 1343, 1349, 1348], ..., 2)
# (451, 0, b'GRID', 8, [ 0, 9, 10, 16, 15, 1343, 1344, 1350, 1349], ..., 2)
# (452, 0, b'GRID', 8, [ 0, 11, 12, 18, 17, 1345, 1346, 1352, 1714], ..., 2)
# (453, 0, b'GRID', 8, [ 0, 12, 13, 19, 18, 1346, 1347, 1353, 1352], ..., 2)
# (454, 0, b'GRID', 8, [ 0, 13, 14, 20, 19, 1347, 1348, 1354, 1353], ..., 2)
# (455, 0, b'GRID', 8, [ 0, 14, 15, 21, 20, 1348, 1349, 1355, 1354], ..., 2)
# (456, 0, b'GRID', 8, [ 0, 15, 16, 22, 21, 1349, 1350, 1356, 1355], ..., 2)
# Print the 2nd row, 1st column in the dataset
print(dataset[1][column_names[0]])
# returns
# 448
# Print the 2nd row, 5th column, 3rd element of the list in the dataset
print(dataset[1][column_names[4]][2])
# returns
# 7
# Same as above, but by using the column name
print(dataset[1]['GRID'][2])
# returns
# 7