将列表转换为Google pub / sub的bytestring

时间:2022-08-07 15:26:34

I am running into an issue while publishing messages to google cloud pub/sub service from a python client.

我在从python客户端向Google Cloud pub / sub服务发布消息时遇到了问题。

I have rest based public API which returns JSON data. The returned data is in dictionary form within in a list.

我有基于rest的公共API,它返回JSON数据。返回的数据在列表中以字典形式存在。

I have extracted the values from the dictionary into a list and using bytes() to convert to the bytestring however it is still throwing the exception below.

我已将字典中的值提取到列表中,并使用bytes()转换为bytestring,但它仍然在下面抛出异常。

['EB', 'Pulaski', '2018-03-06 21:50:18.0', '0.5', '41.7930671862', '41.793140551', '-87.7136071496', 'W', 'Central Park', '-1', '1', '-87.7231602513', '55th']

['EB','Pulaski','2018-03-06 21:50:18.0','0.5','41 .7930671862','41 .793140551',' - 87.7136071496','W','*公园',' - 1','1',' - 87.7231602513','55th']

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

1 个解决方案

#1


0  

Looks like you should use x.encode('utf-8') instead of bytes(x) to convert to bytestring, according to the Python sample here: https://cloud.google.com/pubsub/docs/publisher#publish-messages-to-a-topic

看起来你应该使用x.encode('utf-8')而不是bytes(x)转换为bytestring,根据这里的Python示例:https://cloud.google.com/pubsub/docs/publisher#publish -messages到一个话题

#1


0  

Looks like you should use x.encode('utf-8') instead of bytes(x) to convert to bytestring, according to the Python sample here: https://cloud.google.com/pubsub/docs/publisher#publish-messages-to-a-topic

看起来你应该使用x.encode('utf-8')而不是bytes(x)转换为bytestring,根据这里的Python示例:https://cloud.google.com/pubsub/docs/publisher#publish -messages到一个话题