Ich versuche, ein Skript zu schreiben, das eine spezifische hdf5-Datei liest, die ein Feld namens ElementAbundance
, die arays Bruchwerte für chemische Elemente hat.For Schleife einen einzelnen String-Wert aus einer HDF5-Datei
Dies ist, was ich tue, ich öffne die hdf5-Datei, ich mache eine Liste der Felder in dieser Datei. Wenn die Datei das ElementAbundance
hat, wird es dann überprüfen, ob das Element, das ich in field
an frage, in dem elements
Array ist. Wenn es in dem Array ist, möchte ich dann das Element, das ich gefragt habe, als eine einzelne Zeichenfolge zurückgeben.
Ich habe an anderer Stelle überprüft und die Felder in ElementAbundance
sind
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
Nun dann das ich testen, wenn es den String ‚Wasserstoff‘ gibt, aber ich bin mit der Ausnahme zurückgegeben: Exception: Element type [['Hydrogen']] not found in element abundance list.
Das ist seltsam seit, weil Hydrogen
in der elements
Liste sein sollte, die ich gemacht habe. Außerdem sollte die ausgelöste Ausnahme ['Hydrogen']
nicht [['Hydrogen']]
zurückgeben.
Wenn sie irgendwelche zusätzlichen Informationen sind, die ich hinzufügen kann, lass es mich wissen!