【问题标题】:How to deal with the different type of np.array(list) in py2 and py3 when using pyhdf?使用pyhdf时如何处理py2和py3中不同类型的np.array(list)?
【发布时间】:2019-03-26 15:58:45
【问题描述】:

我想通过 pyhdf 将某些东西保存为 hdf 中的变量。

这是我的代码:

import numpy as np
from pyhdf.SD import *

var = 'PRESSURE_INDEPENDENT_SOURCE'
vartype = 4

hdf4 =  SD('./a.hdf', 2 | 4)

dset = hdf4.create(var, vartype, (1,13))
a = 'AFGL_1976'
b = np.array([list(a.ljust(13))])
dset[:] = b

它在 py2 中工作,b.type|S1

但是,b.dtype 在 py3 中是 <U1,运行代码的最后一行时出现此错误:

TypeError: Cannot cast array data from dtype('<U1') to dtype('S1') according to the rule 'safe'

如果我在 py3 中添加b = b.astype('S1'),也会出现同样的错误。但是,b.dtype|S1

【问题讨论】:

    标签: python python-3.x python-2.7 typeerror pyhdf


    【解决方案1】:

    尝试:

    b = np.array(list(a.ljust(13)),dtype='S1')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多