Created
April 24, 2016 20:56
-
-
Save JamieCressey/a3a75a397db092d7a70bbe876a6fb817 to your computer and use it in GitHub Desktop.
Coverts a standard Python dictionary to a Boto3 DynamoDB item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dict_to_item(raw): | |
if type(raw) is dict: | |
resp = {} | |
for k,v in raw.iteritems(): | |
if type(v) is str: | |
resp[k] = { | |
'S': v | |
} | |
elif type(v) is int: | |
resp[k] = { | |
'I': str(v) | |
} | |
elif type(v) is dict: | |
resp[k] = { | |
'M': dict_to_item(v) | |
} | |
elif type(v) is list: | |
resp[k] = [] | |
for i in v: | |
resp[k].append(dict_to_item(i)) | |
return resp | |
elif type(raw) is str: | |
return { | |
'S': raw | |
} | |
elif type(raw) is int: | |
return { | |
'I': str(raw) | |
} |
@akshowhini
Can you edit your post. for bool, you have a comma instead of a colan.
But thanks all, i was thinking of how to covert my dict to dynamoDB and didnt want to have to write this :)
Thanks, I wasn't able to insert the values in this format:
boto3.version : '1.12.43'
Python 3.6.10 |Anaconda, Inc.| (default, Mar 23 2020, 17:58:33) [MSC v.1916 64 bit (AMD64)] on win32
{'M': {'Row No': {'S': '1'},
'Activity': {'S': 'Playing'},
'Game \n Indoor': {'S': 'Football'},
'TIME_STAMP': {'S': '20200426-155655-8555'},
'ID': {'S': '5305968026208007210'}
}}
or
{'Row No': {'S': '1'},
'Activity': {'S': 'Playing'},
'Game \n Indoor': {'S': 'Football'},
'TIME_STAMP': {'S': '20200426-155655-8555'},
'ID': {'S': '5305968026208007210'}
}
So I made another tweak to get the work done
#To Correct The Keys, If Automated Extraction From Forms
def get_corrected_key(key):
removal_list = [' ', '\t', '\n']
for s in removal_list:
key = key.replace(s, '')
return key
def dict_to_item(raw):
if isinstance(raw, dict):
return {
get_corrected_key(key): dict_to_item(value)
for key, value in raw.items()
}
elif isinstance(raw, list):
return {
[dict_to_item(value) for value in raw]
}
elif isinstance(raw, str):
# Empty Strings are not a valid entry
if len(raw) ==0:
return '-'
return raw
elif isinstance(raw, bool):
return raw
elif isinstance(raw, (int, float)):
return str(raw)
elif isinstance(raw, bytes):
return raw
elif raw is None:
return True
table_work.put_item(
Item=dict_to_item(item))
I wanted to use this with a pandas DataFrame object so I used it like this:
import pandas as pd
def preprocess_df(df):
df.index = df.index.astype('str')
df_dict = df.to_dict()
return df_dict
def dict_to_item(raw):
if isinstance(raw, dict):
return {
'M': {
key: dict_to_item(value)
for key, value in raw.items()
}
}
elif isinstance(raw, list):
return {
'L': [dict_to_item(value) for value in raw]
}
elif isinstance(raw, (str)):
return {'S': raw}
elif isinstance(raw, bool):
return {'BOOL': raw}
elif isinstance(raw, (int, float)):
return {'N': str(raw)}
elif isinstance(raw, bytes):
return {'B', raw}
elif raw is None:
return {'NULL': True}
def df_to_aws_dict(df):
aws_dict = dict_to_item(preprocess_df(df))
return aws_dict
from boto3.dynamodb.types import TypeSerializer
serializer = TypeSerializer()
item = {"foo": "bar"}
dyn_item = {key: serializer.serialize(value) for key, value in item.items()}
from boto3.dynamodb.types import TypeSerializer serializer = TypeSerializer() item = {"foo": "bar"} dyn_item = {key: serializer.serialize(value) for key, value in item.items()}
This really works for me. Thanks a lot, @yousefcodes . I was Stack on it for 1 day. Much Appreciated. Is it possible to do as this works in Update items?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When checking with isinstance() we should take care for the sequence. First check for 'bool' and only then for 'int' and 'float'
because isinstance(raw, (int, float)) returns True for boolen values: