2017-04-13 51 views
0

我想將一些JSON對象和兩個整數傳遞給一個池。Python TypeError - 所需的類似字節的對象,而不是str

for i in range(0, multiprocessing.cpu_count()-1): 
    fromindex = i * chunklen 
    toindex = (i+1) * chunklen 
    chunkedData.append([data['features'][fromindex:toindex], weekdaytopredict, hourtopredict]) 
chunkedData.append([data['features'][toindex:], weekdaytopredict, hourtopredict]) 
parallelstart = time.time() 
result = (pool.map(parallelUpdateWithDT, chunkedData)) 

data是帶有一些多邊形的geoJSON文件。我想分發這些多邊形進行並行處理。我將n/cpu_count()多邊形傳遞給parallelUpdateWithDT函數,該函數應該進一步處理它們。我的問題是typeerror:即使print(chunkedData)返回<class 'list'>,我geth以下錯誤:TypeError: a bytes-like object is required, not 'str'。我在哪裏搞砸了?完整堆棧跟蹤:

--------------------------------------------------------------------------- 
RemoteTraceback       Traceback (most recent call last) 
RemoteTraceback: 
""" 
Traceback (most recent call last): 
    File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker 
    result = (True, func(*args, **kwds)) 
    File "/usr/lib/python3.5/multiprocessing/pool.py", line 44, in mapstar 
    return list(map(*args)) 
    File "<ipython-input-114-bf56cacb90b9>", line 34, in parallelUpdateWithDT 
    if('rain' in result): 
TypeError: a bytes-like object is required, not 'str' 
""" 

The above exception was the direct cause of the following exception: 

TypeError         Traceback (most recent call last) 
<ipython-input-115-031a5e24ee66> in <module>() 
----> 1 decisionTreePrediciton(3, 5) 

<ipython-input-114-bf56cacb90b9> in decisionTreePrediciton(weekdaytopredict, hourtopredict) 
    15  print (type(chunkedData)) 
    16 
---> 17  result = (pool.map(parallelUpdateWithDT, chunkedData)) 
    18  parallelend = time.time() 
    19 

/usr/lib/python3.5/multiprocessing/pool.py in map(self, func, iterable, chunksize) 
    258   in a list that is returned. 
    259   ''' 
--> 260   return self._map_async(func, iterable, mapstar, chunksize).get() 
    261 
    262  def starmap(self, func, iterable, chunksize=None): 

/usr/lib/python3.5/multiprocessing/pool.py in get(self, timeout) 
    606    return self._value 
    607   else: 
--> 608    raise self._value 
    609 
    610  def _set(self, i, obj): 

chunkedData樣本:

[[[{'geometry': {'coordinates': [[[10.914622377957983, 45.682007076150505], [10.927456267537572, 45.68179119797432], [10.927147329501077, 45.672795442796335], [10.914315493899755, 45.67301125363092], [10.914622377957983, 45.682007076150505]]], 'type': 'Polygon'}, ///////////////////////etc, waaay too big////////////, 'id': 6574, 'properties': {'cellId': 11454}}], 3, 5] 

這是怎麼一個str?我不明白。謝謝你的幫助!

+0

工作人員在'if('rain'in結果):'並將其傳播給父母並在那裏重新提出,讓其知道發生了什麼。問題在於這裏沒有發佈的工作者代碼。我猜'結果'是一個不在塊中的計算值。您可以在該行的上方添加「print(repr(result))」,並查看您獲得的內容。但是由於這個錯誤代碼沒有發佈,所以我們不能說太多。 – tdelaney

回答

3

從您發佈的代碼中無法分辨出來,但我懷疑您試圖檢查str是否爲in a bytes。例如:

>>> bytes_obj = b'result' 
>>> 'res' in bytes_obj 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: a bytes-like object is required, not 'str' 

這意味着result在你的代碼是bytes類型。這裏有兩個解決方案。首先是使'rain'成字節對象太:

if b'rain' in result: 
    ... 

二是把resultstr

result = result.decode(whatever_codec_it_should_be) 

如果你打算採取第二種方法,你應該轉換導致str在儘可能早的時候,以避免各種strbytes頭痛。通常,如果你不知道知道你需要一個不同的編解碼器,現在大部分工作都是從utf-8這幾天開始的,所以你可以試試那個...

相關問題