Bar*_*rkz 9 python decimal amazon-dynamodb
我试图从我的 dynamodb 表中删除一个项目,但是我收到了这个异常:
decimal.Inexact: [<class 'decimal.Inexact'>, <class 'decimal.Rounded'>]
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决它,有什么帮助吗?这是代码:
dynamodb = boto3.resource('dynamodb', region_name='***')
table = dynamodb.Table('***')
propid = 1234
column_set = Decimal(44.30912302164986)
print("Attempting a conditional delete...")
try:
response = table.delete_item(
Key={
'hashkey': propid,
'rangekey': column_set
},
)
except ClientError as e:
if e.response['Error']['Code'] == "ConditionalCheckFailedException":
print(e.response['Error']['Message'])
else:
raise
else:
print("DeleteItem succeeded:")
Run Code Online (Sandbox Code Playgroud)
后期编辑:
当我这样做时,它似乎没有正确存储:
year = decimal.Decimal(44.309123021649860)
Run Code Online (Sandbox Code Playgroud)
它打印:
44.30912302164986016350667341612279415130615234375
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会发生这种情况,但对于它的价值,您可以执行类似以下操作(基于boto3 问题线程中找到的评论):
with decimal.localcontext(boto3.dynamodb.types.DYNAMODB_CONTEXT) as ctx:
ctx.traps[decimal.Inexact] = False
ctx.traps[decimal.Rounded] = False
column_set = ctx.create_decimal_from_float(v)
response = table.delete_item(
Key={
'hashkey': propid,
'rangekey': column_set
},
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6040 次 |
| 最近记录: |