2016-07-26 42 views
1

事情我想做的事:

將字符串轉換較百分比XX%至0和1NumPy的I/O:轉換%比例上浮0和1之間

我的代碼之間的浮動:

#a. general case 
data = "1, 2.3%, 45.\n6, 78.9%, 0" 
names = ("i", "p", "n") 
a = np.genfromtxt(io.BytesIO(data.encode()), names = names, delimiter = ",") 
print (a)   # returns [(1.0, nan, 45.0) (6.0, nan, 0.0)] 
print (a.dtype)  # reason: default dtype is float, cannot convert 2.3%, 78.9% 


#b. converter case 
convertfunc = lambda x: float(x.strip("%"))/100  # remove % and return the value in float (between 0 and 1) 
b = np.genfromtxt(io.BytesIO(data.encode()), names = names, delimiter = ",", converters = {1:convertfunc}) # use indices for 2nd column as key and do the conversion 
print (b) 
print (b.dtype) 

我的問題:

在一般情況下,在%的比例將被打印成南。由於故障dtype爲浮點數,因此無法轉換百分比。因此,我嘗試了轉換器方法。

然而,當我運行的代碼,就會發生錯誤:

convertfunc = lambda x: float(x.strip("%"))/100  # remove % and return the value in float (between 0 and 1) 
TypeError: a bytes-like object is required, not 'str' 

任何人都知道這裏有什麼問題嗎? (我正在使用python3.5)

謝謝你的任何答案。

回答

1

不能將類似字節的對象與str對象即'%'分開。追加b字符串的開頭,使其成爲字節對象。

convertfunc = lambda x: float(x.strip(b"%"))/100 
#         ^

b = np.genfromtxt(io.BytesIO(data.encode()), names = names, delimiter = ",", converters = {1:convertfunc}) 

print(b) 
# array([(1.0, 0.023, 45.0), (6.0, 0.789, 0.0)], 
# dtype=[('i', '<f8'), ('p', '<f8'), ('n', '<f8')]) 

與一家領先的b這樣的對象屬於bytes類:

>>> type('%') 
<class 'str'> 
>>> type(b'%') 
<class 'bytes'> 
+0

非常感謝你。這真的回答了我的問題。 –