For 循环来自 HDF5 文件的单个字符串值

钢铁侠

我正在尝试编写一个脚本来读取具有名为 的字段的特定 hdf5 文件,该文件具有ElementAbundance化学元素的 araays 分数值。

这就是我正在做的,我打开 hdf5 文件,我列出了该文件中的字段。如果文件有ElementAbundance,它将检查我要求输入的元素field是否在elements数组中。如果它在数组中,那么我想将我要求的元素作为单个字符串返回。

我在其他地方检查过,其中的字段ElementAbundance

elements = ['Carbon', 'Helium', 'Hydrogen', 'Iron', 'Magnesium', 'Neon', 'Nitrogen', 'Oxygen', 'Silicon']

import h5py
from particleType import partTypeNum # This is another file that is unimportant in regards to my question

# Only necessary if gas (0) particle type

def loadElement(basePath,snapNum,partType,field=None):
    result = {}

    # This uses the above module to associate keys words with the letter 0    
    ptNum = partTypeNum(partType)
    gName = "PartType" + str(ptNum)

    # making sure fields is not a single element
    if isinstance(field, basestring):
        field = [field]

    # begin by opening the h5py file
    with h5py.File(snapPath(basePath,snapNum),'r') as f:

        # header = dict( f['Header'].attrs.items() )
        # nPart = getNumPart(header)

        # This creates a list for all the fields in the HDF5 file
        field_list = []
        for i in f[gName].keys():
            field_list.append(str(i))

        # This will check if the file has a "ElementAbundance" header
        for i in enumerate(field_list):
            # if the string is not inside the list, we raise an exception
            if "ElementAbundance" not in field_list:
                raise Exception("Particle type ["+str(ptNum)+"] does not have a field of elements")
            # If it is, we extract the chemical elements from inside the element abundance field.
            else:
                g = f[gName]['ElementAbundance'] # file contains elements
                elements = []
                for j in g.keys():
                    elements.append(str(j))


        # now for looping the lists values with their index
        for i,element in enumerate(elements):
            # if the element field is inside the elements list, we retrieve that element as a string
            if field == element:
                the_element = str(elements[i])
                return the_element
            # if their is a ElementAbundance field but the asked for field element is not in that list, raise and exception.
            else:
                raise Exception("Element type ["+str(field)+"] not found in element abundance list.")

        f.close()
    # testing to see if the above for loop returns a single string
    return the_element 

现在我然后测试它是否返回字符串“Hydrogen”,但我返回异常: Exception: Element type [['Hydrogen']] not found in element abundance list.

这很奇怪,因为Hydrogen应该在elements我制作列表中。此外,引发的异常应该返回['Hydrogen']not [['Hydrogen']]

如果他们是我可以添加的任何其他信息,请告诉我!

他沉默了

如果您将函数调用为:

loadElement(basePath,snapNum,partType,field='Hydrogen')

然后 'Hydrogen' 变成了一个单项列表:

if isinstance(field, basestring):
        field = [field]

当您迭代元素列表时,您正在迭代字符串并将它们与['Hydrogen']列表匹配,因此找不到匹配项。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

循环播放HDF5文件列表

来自分类Dev

读取HDF5文件

来自分类Dev

HDF5的C ++头文件丢失

来自分类Dev

Java-从循环返回多个值,作为单个字符串

来自分类Dev

如何使用逗号在单个字符串中循环 10 次值?

来自分类Dev

HDF5将字符串标头写入文件

来自分类Dev

窥探熊猫的hdf5文件中的行数

来自分类Dev

在Julia中附加或创建HDF5文件

来自分类Dev

HDF5文件中名称中的'/'混淆

来自分类Dev

重命名HDF5文件中的列

来自分类Dev

MATLAB中无限hdf5文件的效率

来自分类Dev

Github-如何下载HDF5文件?

来自分类Dev

ModelCheckpoint不保存hdf5文件

来自分类Dev

重命名HDF5文件中的列

来自分类Dev

将hdf5文件转换为灰度

来自分类Dev

从 HDF5 文件读取和写入 numpy 数组

来自分类Dev

遍历 HDF5 文件/树并在返回后继续?

来自分类Dev

如何使用 macports 安装 HDF5、openMPI .mod 文件?

来自分类Dev

hdf5设计人员是否解决了与打开.hdf5文件有关的损坏问题?

来自分类Dev

来自查询的值在Jinja中显示为元组,但应为单个字符串

来自分类Dev

如何使用h5py从HDF5数据集中读取字符串

来自分类Dev

HDF5存储具有不同大小的字符串属性

来自分类Dev

如何在HDF5中写入定长字符串?

来自分类Dev

从HDF5数据集中读取字符串数组

来自分类Dev

在 HDF5/C++ 中读取字符串

来自分类Dev

更改字符串中的单个字符无法循环工作

来自分类Dev

python中是否有单个函数可以显示.hdf5文件的完整结构?

来自分类Dev

Python熊猫使用read_hdf和HDFStore.select从HDF5文件读取特定值

来自分类Dev

Python熊猫使用read_hdf和HDFStore.select从HDF5文件读取特定值