使用Group.visititems时,是否可以获取数据集或组的父级?

时间:2019-03-05 02:57:54

标签: python python-3.x h5py

我正在尝试将h5py File对象放入树结构中,以便可以使用它的功能打印出树的表示形式,以与linux“ tree”命令递归显示的方式相同的方式显示文件的内容。目录的内容。递归访问文件中所有项目的最佳方法是使用Group.visititems方法,并传入将用于向树中添加节点的函数。这是我到目前为止的内容:

def zip(A, B):
    #Base case to break out prior to running.
    if A < 0 or B < 0 or A > 100000000 or B > 100000000:
        return -1
    else:
        C = ""
        count = 0
        Astr = str(A)
        Bstr = str(B)
        #Iterate through A
        for a in Astr:
            C += a
            #If values still remain in B
            if not count >= len(Bstr):
                C += Bstr[count]
                count += 1
        #If we are done with A, then we can continue with only B
        if count < len(Bstr):
            for b in range(count, len(Bstr)):
                C += Bstr[b]
        return C

1 个答案:

答案 0 :(得分:1)

在意识到Dataset和Group类都确实具有父属性(hpaulj在对该问题的评论中也指出)并清除了一些数据之后,我能够获得所需的输出: / p>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="accordion parent" id="accord1">
  <div class="card">
    <div class="card-header" id="head1">
      <h5 class="mb-0">
        <button class="btn btn-block btn-dark collapsed" type="button" data-toggle="collapse" data-target="#col1" aria-expanded="true" aria-controls="col1">
					
				</button>
      </h5>
    </div>

    <div id="col1" class="collapse" aria-labelledby="head1" data-parent="#accord1">
      <div class="card-body">
        <div class="accordion" id="accord1_1">
          <div class="card">
            <div class="card-header" id="head1_1">
              <h5 class="mb-0">
                <button class="btn btn-block btn-dark collapsed" type="button" data-toggle="collapse" data-target="#col1_1" aria-expanded="true" aria-controls="col1_1">
									
								</button>
              </h5>
            </div>

            <div id="col1_1" class="collapse" aria-labelledby="head1_1" data-parent="#accord1_1">
              <div class="card-body">
                <div class="accordion" id="accord_2">
                  <div class="card">
                    <div class="card-header" id="head2">
                      <h5 class="mb-0">
                        <button class="btn btn-block btn-dark collapsed" type="button" data-toggle="collapse" data-target="#col2" aria-expanded="true" aria-controls="col2">
													
												</button>
                      </h5>
                    </div>

                    <div id="col2" class="collapse" aria-labelledby="head2" data-parent="#accord_2">
                      <div class="card-body">

                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="card">
            <div class="card-header" id="head1_2">
              <h5 class="mb-0">
                <button class="btn btn-block btn-dark collapsed" type="button" data-toggle="collapse" data-target="#col1_2" aria-expanded="true" aria-controls="col1_2">
									
								</button>
              </h5>
            </div>

            <div id="col1_2" class="collapse" aria-labelledby="head1_2" data-parent="#accord1_1">
              <div class="card-body">

              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Dataset和Group类的name属性显然提供了完整的hdf5路径,因此我使用一些os.path函数对其进行了清理。