Python 将列表转换为Google发布/订阅的bytestring
我在从python客户端向google云发布/订阅服务发布消息时遇到了一个问题 我有一个基于rest的公共API,它返回JSON数据。返回的数据是列表中的字典形式 我已经将字典中的值提取到一个列表中,并使用bytes()将其转换为bytestring,但是它仍然抛出下面的异常Python 将列表转换为Google发布/订阅的bytestring,python,python-3.x,google-cloud-platform,Python,Python 3.x,Google Cloud Platform,我在从python客户端向google云发布/订阅服务发布消息时遇到了一个问题 我有一个基于rest的公共API,它返回JSON数据。返回的数据是列表中的字典形式 我已经将字典中的值提取到一个列表中,并使用bytes()将其转换为bytestring,但是它仍然抛出下面的异常 ['EB'、'Pulaski'、'2018-03-06 21:50:18.0'、'0.5'、'41.7930671862'、'41.793140551'、'-87.7136071496'、'W'、'Central Park
['EB'、'Pulaski'、'2018-03-06 21:50:18.0'、'0.5'、'41.7930671862'、'41.793140551'、'-87.7136071496'、'W'、'Central Park'、'-1'、'1'、'-87.7231602513'、'55']
response = requests.get("https://data.cityofchicago.org/resource/8v9j-bter.json")
traffic = response.json()
result_list = []
for d in traffic:
result_list.append([v for k, v in d.items()])
for x in result_list:
print(x)
publisher.publish(topic_path, data = bytes(x))
看起来您应该使用x.encode('utf-8')而不是字节(x)来转换为ByTestString,根据这里的Python示例:什么异常?pub/sub必须作为ByTestString发送