簡體   English   中英

他們在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

特別:

當用於生成dtype對象時,幾種python類型等效於對應的數組標量:

int int_

布爾布爾_

float_

復雜的浮點

字符串

unicode unicode_

緩沖區無效

(所有其他)object_

如上面的注釋所建議,.astype(str)應該起作用。

編輯

實際上,此信息來自Python2.7,我也嘗試過此操作,並且unicode不起作用,但是astype(str)默認為unicode(就像在python3中所期望的那樣)。 但是有趣的是,由於這些字符串代碼似乎起作用:

'b'布爾值

'i'(有符號)整數

'u'無符號整數

'f'浮點數

“ c”復雜浮點

'O'(Python)對象

'S','a'(字節)字符串

'U'Unicode

“ V”原始數據(無效)

有關python3和python2中的字符串的信息(可能是最重要的更改)在這里:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM