Python类型错误 - 需要 bytes-like 对象而不是字符串

2024-04-20 14:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一些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函数,该函数将进一步处理它们。我的问题是一个typeerror:即使print(chunkedData)返回<class 'list'>,我还是得到了以下错误:TypeError: a bytes-like object is required, not 'str'。我在哪里搞砸了?完整堆栈跟踪:

^{pr2}$

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]

这是怎么回事?我不明白。谢谢你的帮助!在


Tags: 函数datatimecountcpu多边形featuresappend
1条回答
网友
1楼 · 发布于 2024-04-20 14:31:38

从您发布的代码中无法判断,但我怀疑您试图检查str是否是inabytes。例如:

>>> 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'也变成bytes对象:

^{pr2}$

第二种方法是将result转换为str

result = result.decode(whatever_codec_it_should_be)

如果您打算采用第二种方法,您应该尽早将结果转换为str,以避免str与{}之间的各种头痛。通常,如果你不知道你需要一个不同的编解码器,那么现在大多数东西都是用^{来工作的,所以你可以试试这个。。。在

相关问题 更多 >