2016-01-13 22 views
1

因爲(我認爲)升級到python 3.5(從2.7),我得到了一個奇怪的錯誤他們改變了python 3中的astype('string')嗎?

當試圖打開一個文件(充滿位串)和他們操縱這樣的字符串:

#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 

這只是一個愚蠢的修復 (我希望)。提前致謝。

+2

什麼用astype發生(STR) ? – pvg

+0

解決了它,謝謝@pvg – cancerconnector

回答

1

這裏是D型細胞上選項的信息:​​ 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串VS python2(可能是最顯著的變化)信息是在這裏:

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

+0

'astype(unicode)'可能工作得更好,因爲這是Python 3中非裝飾字符串文字的類型。 –

+0

剛剛嘗試過,它不起作用... CA = np .array(x).astype(unicode) NameError:name'unicode'未定義 – cancerconnector

+0

請參閱編輯,astype(str)和astype(「U」)導致相同的數組,而astype(「S」)將數據作爲字節字符串 –

相關問題