简体   繁体   English

无法解析 AWS DynamoDB 二进制获取 boto3 python 中的项目

[英]Unable to parse AWS DynamoDB binary get item in boto3 python

I have got the items from AWS dynamodb as dict which has a value in binary format.我从 AWS dynamodb 获得了作为 dict 的项目,它具有二进制格式的值。 I am not able to retrieve the value of a binary content.我无法检索二进制内容的值。

Below is the sample.下面是示例。

my_details  = {'route': Binary(b'gAAAABgLNW9tNcpeclIy1LSs8wKYRy9uMxgr5V4TwJmEJNZ2WVlb3Z3LtIK3PewO2SDRYkvXAh8bcZ4Ej_jBjaNi8xhU1-P2FLpcGEX2g='), 'way': '5064', '
stop': Binary(b'\xf1J\xef\xa0\xac\xb1A0\xa9\\:'), 'name': 'cfcf57'}

print(type(my_details['route']))

print(my_details['route'])
print(my_details['way'])

I need value of the route key like below我需要如下route键的值

gAAAABgLNW9tNcpeclIy1LSs8wKYRy9uMxgr5V4TwJmEJNZ2WVlb3Z3LtIK3PewO2SDRYkvXAh8bcZ4Ej_jBjaNi8xhU1-P2FLpcGEX2g=

I have tried to get the value of route using mydetails['route'] but got below error我尝试使用mydetails['route']获取route的值,但出现以下错误

<class 'boto3.dynamodb.types.Binary'>
Traceback (most recent call last):
  File "C:/Users/Prabhakar/Documents/Projects/Test.py", line 18, in <module>
    print(my_details['route'])
    
TypeError: __str__ returned non-string (type bytes)

Please let me know how can I retrieve the binary content in python dict.请让我知道如何检索 python 字典中的二进制内容。

It is returning data with byte type, to get it in str type, you have to call .decode method on it.它返回byte类型的数据,要获取它的str类型,你必须调用它的.decode方法。 Here is the documentation for that: https://docs.python.org/3/library/stdtypes.html#bytes.decode这是相关的文档: https://docs.python.org/3/library/stdtypes.html#bytes.decode

print(bytes(my_details['route']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM