2017-04-13 181 views
0

일부 JSON 객체와 두 개의 정수를 풀에 전달하려고합니다.Python TypeError - str 대신 bytes-like 객체가 필요합니다.

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 함수에 전달합니다.이 함수는 추가 처리해야합니다. 내 문제는 형식 오류입니다. 이 <class 'list'>을 반환하더라도 다음 오류가 발생합니다 : 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 결과) :'그것은 부모에게 다시 전파되어 그곳에서 다시 일어 났고 어떤 일이 일어 났는지 알려줍니다. 문제는 여기에 게시되지 않은 작업자 코드에 있습니다. 나는'result'가 청크가 아닌 계산 된 값이라고 추측합니다. 이 줄 바로 위에'print (repr (result)) '를 추가하여 얻은 것을 볼 수 있습니다. 그러나 버그가 게시되지 않은 코드에 있기 때문에 우리는 그것에 대해 너무 많이 말할 수는 없습니다. – tdelaney

답변

3

게시 한 코드에서 알 수없는 내용이지만 strin 인 경우 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이다 것을 의미한다. 여기에는 두 가지 결의안이 있습니다. 이 두 번째 방법을 위하여려고하는 경우에

result = result.decode(whatever_codec_it_should_be) 

, 당신은 변환해야합니다 :

if b'rain' in result: 
    ... 

두 번째는 str 것은으로 result을 설정하는 것입니다 : 첫 번째는 너무 바이트 객체로 'rain'를 확인하는 것입니다 strbytes 두통의 모든 종류를 피할 수있는 가능한 가장 빠른 순간에 str의 결과. 일반적으로 을 알고있는 경우 다른 코덱이 필요하므로 요즘은 utf-8에서 작동하므로 그 중 하나를 시도해 볼 수 있습니다 ...