【问题标题】:Python HDF5 H5Py issues opening multiple filesPython HDF5 H5Py 问题打开多个文件
【发布时间】:2011-12-02 07:33:02
【问题描述】:

我使用 64 位版本的 Enthought Python 来处理跨多个 HDF5 文件的数据。我在 64 位 Windows 上使用 h5py 版本 1.3.1 (HDF5 1.8.4)。

我有一个对象,它为我的特定数据层次结构提供了一个方便的接口,但是单独测试 h5py.File(fname, 'r') 会产生相同的结果。我正在遍历一个长列表(一次约 100 个文件)并尝试从文件中提取特定的信息。我遇到的问题是我从几个文件中获取了相同的信息!我的循环看起来像:

files = glob(r'path\*.h5')
out_csv = csv.writer(open('output_file.csv', 'rb'))

for filename in files:
  handle = hdf5.File(filename, 'r')
  data = extract_data_from_handle(handle)
  for row in data:
     out_csv.writerow((filename, ) +row)

当我使用 hdfview 之类的工具检查文件时,我知道内部结构不同。但是,我得到的 csv 似乎表明所有文件都包含相同的数据。有没有人见过这种行为?有什么建议可以让我开始调试这个问题?

【问题讨论】:

    标签: python hdf5 h5py


    【解决方案1】:

    我已经得出结论,这是 Perplexing assignment behavior with h5py object as instance variable 的一种奇怪表现。我重新编写了我的代码,以便在函数调用中处理每个文件并且不重用变量。使用这种方法,我看不到同样的奇怪行为,而且它似乎工作得更好。为清楚起见,解决方案看起来更像:

    files = glob(r'path\*.h5')
    out_csv = csv.writer(open('output_file.csv', 'rb'))
    
    def extract_data_from_filename(filename):
        return extract_data_from_handle(hdf5.File(filename, 'r'))
    
    for filename in files:
      data = extract_data_from_filename(filename)
      for row in data:
         out_csv.writerow((filename, ) +row)
    

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 1970-01-01
      • 2012-01-19
      • 2023-04-06
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2015-09-17
      相关资源
      最近更新 更多