简体   繁体   中英

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

gAAAABgLNW9tNcpeclIy1LSs8wKYRy9uMxgr5V4TwJmEJNZ2WVlb3Z3LtIK3PewO2SDRYkvXAh8bcZ4Ej_jBjaNi8xhU1-P2FLpcGEX2g=

I have tried to get the value of route using mydetails['route'] but got below error

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

It is returning data with byte type, to get it in str type, you have to call .decode method on it. Here is the documentation for that: https://docs.python.org/3/library/stdtypes.html#bytes.decode

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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