如何在使用pyhdf时处理py2和py3中不同类型的np.array(list)?

2024-05-14 04:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过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


Tags: 代码fromimportvarnppy3sdarray