gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:57 25 4
gpt4 key购买 nike

自从(我认为)升级到 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

关于python - 他们在 python 3 中更改了 astype ('string' ) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com