[英]How to deal with the different type of np.array(list) in py2 and py3 when using pyhdf?
我想通过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
是b.dtype
中的<U1
,运行代码的最后一行时出现此错误:
TypeError: Cannot cast array data from dtype('<U1') to dtype('S1') according to the rule 'safe'
如果我在b = b.astype('S1')
添加b = b.astype('S1')
,则会出现相同的错误。 但是, b.dtype
是|S1
。
尝试:
b = np.array(list(a.ljust(13)),dtype='S1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.