【问题标题】:Python TypeError - required bytes-like object instead of strPython TypeError - 需要类似字节的对象而不是 str
【发布时间】:2017-04-14 00:39:01
【问题描述】:

我正在尝试将一些 JSON 对象和两个整数传递到池中。

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 函数,它应该进一步处理它们。我的问题是类型错误:即使print(chunkedData) 返回<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?我不明白。感谢您的帮助!

【问题讨论】:

  • 工人在if('rain' in result): 提出了一个错误,该错误被传播回父级并在那里重新引发以让它知道发生了什么。问题出在此处未发布的工作人员代码中。我猜result 是一个不在块中的计算值。您可以在该行上方添加print(repr(result)),看看您会得到什么。但由于 bug 出现在未发布的代码中,我们不能说太多。

标签: python python-3.x multiprocessing typeerror


【解决方案1】:

从您发布的代码中无法判断,但我怀疑您正在尝试检查 str 是否为 inbytes。例如:

>>> 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'

这意味着您的代码中的resultbytes 类型。这里有两个决议。首先是把'rain'也变成一个字节对象:

if b'rain' in result:
    ...

第二个是将result变成str

result = result.decode(whatever_codec_it_should_be)

如果您打算采用第二种方法,则应尽早将结果转换为str,以避免出现各种strbytes 之间的问题。通常,如果您知道您需要一个不同的编解码器,现在大多数事情都在utf-8 上工作,所以您可以尝试那个...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多