他们在 python 3 中更改了 astype('string') 吗?

did they change astype('string') in python 3?

自从(我认为)升级到 python 3.5(从 2.7)

以来,我在一些非常基本的代码中遇到了一个奇怪的错误

当试图打开一个文件(充满位串)时,他们像这样操作字符串:

#bit string data
data = open(read_path+'genomes'+str(time)).read().replace(',','\n').replace('\n','')
x = data.split()
CA = np.array(x).astype('string')
Genomes = np.reshape(CA, (size,size))
genomelength = len(Genomes[0][0])
for entry in range(0, size**2): total_mut1[entry] = np.array(sum_digits(CA[entry])).astype('int')
mut_array1 = np.reshape(total_mut1, (size,size))

升级前有效...

我现在收到这个错误:

CA = np.array(x).astype('string')
TypeError: data type "string" not understood

这只是一个愚蠢的修复吗(我希望如此)。提前致谢。

以下是有关 dtype 选项的信息: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

具体来说:

Several python types are equivalent to a corresponding array scalar when used to generate a dtype object:

int int_

bool bool_

float float_

complex cfloat

str string

unicode unicode_

buffer void

(all others) object_

正如上面的评论所建议的,.astype(str) 应该有效。

编辑

实际上这个信息取自Python2.7,我也试过这个,但unicode没有用,但是astype(str)默认为unicode(正如人们在python3中所期望的那样)。不过很有趣,因为这些字符串代码似乎有效:

'b' boolean

'i' (signed) integer

'u' unsigned integer

'f' floating-point

'c' complex-floating point

'O' (Python) objects

'S', 'a' (byte-)string

'U' Unicode

'V' raw data (void)

关于 python3 与 python2 中的字符串的信息(可能是最重要的变化)在这里:

https://docs.python.org/3.0/whatsnew/3.0.html