我在下面的代码中尝试将uuid转换为字符串,但我总是遇到错误。无论我是否与uuid.uuid4()分别声明str() 请参阅下面的代码:
from __future__ import print_function
from decimal import *
import boto3
import json
from locale import str
import uuid
def my_handler(event, context):
description = event['description']
spot_id = uuid.uuid4() #Unique identifier for spot
dynamodb = boto3.client('dynamodb')
tablesinfo = "sinfo"
dynamodb.put_item(
TableName = tablesinfo, Item = {
'spot_id':{'S' : str(spot_id)},
'description': {'S' : description
}
)
return {'spot_id' : spot_id}
这些是我收到的错误:
{
"stackTrace": [
[
"/var/task/Create_Spot_Test.py",
15,
"my_handler",
"'spot_id':{'S' : str(spot_id)},"
],
[
"/usr/lib64/python2.7/locale.py",
303,
"str",
"return format(\"%.12g\", val)"
],
[
"/usr/lib64/python2.7/locale.py",
196,
"format",
"return _format(percent, value, grouping, monetary, *additional)"
],
[
"/usr/lib64/python2.7/locale.py",
202,
"_format",
"formatted = percent % value"
]
],
"errorType": "TypeError",
"errorMessage": "float argument required, not UUID"
}
答案 0 :(得分:1)
from locale import str
(导入以前的错误)
此外,您必须先将uuid = uuid.uuid4()
声明为变量,然后声明另一个变量,将其转换为字符串spot_id = str(uuid)
,而不是内联str()
。