2

print(list(file.keys()))

When I run this code I get:

T00000000,T00000001,T00000002,T00000003, ... ,T00000474

Now, I analized T00000000 but I want to scan them all with for loop. I couldn't do because this is a string. Is there any way to do this?

kcw78
  • 7,131
  • 3
  • 12
  • 44

2 Answers2

3

@python_student, there is more to this than explained in the initial answer. Based on the syntax of you question, it appears you are using h5py to read the HDF5 file. To effectively access the file contents, you need a basic understanding of HDF5 and h5py. I suggest starting here: h5py Quick Start Guide. In addition, there are many good questions and answer here on StackOverflow with details and examples.

An HDF5 file has 2 basic objects:

  • Datasets: array-like collections of data
  • Groups: folder-like containers that hold datasets and other groups

h5py, uses dictionary syntax to access Group objects, and reads Datasets using NumPy syntax. (Note group objects are not Python dictionaries - just just "look" like them!)

As you noted, the keys() are the NAMES of the objects (groups or datasets) at the root level of your file. Your code created a list from the group keys: list(file.keys()). In general there is no reason to do this. Typically, you will iterate over the keys() or items() instead of creating a list.

Here is a short code segment to show how you might do this. I can add more details once I know more about your data schema. (HDF5 is a general data container and have most any schema.)

# loop on names:
for name in file.keys():
    print(name)
# loop on names and H5 objects:
for name, h5obj in file.items():
    if isinstance(h5obj,h5py.Group):
        print(name,'is a Group')
    elif isinstance(h5obj,h5py.Dataset):
        print(name,'is a Dataset')
        # return a np.array using dataset object:
        arr1 = h5obj[:]
        # return a np.array using dataset name:
        arr2 = file[name][:] 
        # compare arr1 to arr2 (should always return True):
        print(np.array_equal(arr1, arr2))
kcw78
  • 7,131
  • 3
  • 12
  • 44
1

Yes, you can use the split() method.

If the string is "T00000000,T00000001,T00000002,T00000003, ... ,T00000474", you can use split to turn it on a list like this:

string = "T00000000,T00000001,T00000002,T00000003, ... ,T00000474"
values = string.split(",")

So, the list values becomes ["T00000000", "T00000001","T00000003", ... ,"T000000474"].

Then you can use this in a for loop.

If you don't wanto to create a list, you can simply:

for value in string.split(","):
     #Your code here...

The for loop will be execute with the values T00000000, T00000001, T00000003 ...

4s7r0n4u7
  • 56
  • 3