I'm using Python 3.7 to store data in a DynamoDB database and encountering the following error message when I try and write an item to the database:
Float types are not supported. Use Decimal types instead. My code:
ddb_table = my_client.Table(table_name) with ddb_table.batch_writer() as batch: for item in items: item_to_put: dict = json.loads(json.dumps(item), parse_float=Decimal) # Send record to database. batch.put_item(Item=item_to_put) "items" is a list of Python dicts. If I print out the types of the "item_to_put" dict, they are all of type str.
Thanks in advance for any assistance.
24 Answers
Convert all the float to the Decimal
import json from decimal import Decimal item = json.loads(json.dumps(item), parse_float=Decimal) table.put_item( Item=item ) 2Ran into the same issue and it turned out that Python was passing a string parameter as something else. The issue went away when I wrapped all of the items in str().
2Same issue here. In my case it was because some of my records did not have a certain key, and that key was being auto-added with nan by default(I guess?). I added some simple logic to check if that key was a list and set it to an empty list if not before running batch.put_item which has seemingly satisfied the Dynamo beast.
Add: from decimal import Decimal above the expression. This should fix the problem