Code snippet :
conn = dynamo_connect()
company = Table("companydb",connection=conn)
companyrecord = {'company-slug':'www-google-com12','founding-year':1991, 'randomlist' :[1,2,3,4,5], 'randomdict' : {'a':[1,2,3],'b':'something','randomnumber':10.55} }
company.put_item(data=companyrecord)
I am getting the following error:
File "C:\Python27\lib\site-packages\boto\dynamodb2\items.py", line 329, in prepare_full
final_data[key] = self._dynamizer.encode(value)
File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 279, in encode
return {dynamodb_type: encoder(attr)}
File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 335, in _encode_m
return dict([(k, self.encode(v)) for k, v in attr.items()])
File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 279, in encode
return {dynamodb_type: encoder(attr)}
File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 305, in _encode_n
raise DynamoDBNumberError(msg)
boto.dynamodb.exceptions.DynamoDBNumberError: BotoClientError: Inexact numeric for `10.55`
There is two methods: float_number = float ( decimal_number ) float_number = decimal_number * 1.0.
There are many ways to set the precision of the floating-point values. Some of them are discussed below. Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
The floating-point calculations are inaccurate because mainly the rationals are approximating that cannot be represented finitely in base 2 and in general they are approximating numbers which may not be representable in finitely many digits in any base.
If you are working on a larger set and want to avoid record-wise processing to convert decimal, this would help.
from decimal import Decimal
import json
changed_data = json.loads(json.dumps(data), parse_float=Decimal)
Yes There are Known issues on GitHub related to floating numbers, There may be 2 workarounds , First if you are comfortable to store 10.5
instead of 10.55
, then it will just work fine I guess, The another is to store the floating value as String or integer and later modulate it while accessing.
So of you chose the string part then you could store it as '10.55'
instead of 10.55
and later when you access the values from the table then you could simply use float("10.55")
and you will be done.
Another method is to store it as an integer , First choose a precision value (say 2 decimal values) then you will store 10.55
as 1055
(multiplied by 100, since 2 decimal precision), and while accessing it you could have simply used 1055/100.0
and you will get 10.55
.
Use Decimal(str(your_number)) instead. See https://github.com/boto/boto3/issues/665
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With