python - 如何在 AWS Lambda 中将浮点值作为输入?
问题描述
我正在尝试将 AWS lambda 中的浮点值作为键、值对在事件中传递,为此我正在做
from decimal import Decimal
ENERGY_VERBRAUCH = event['energy_verbrauch']
ENERGY_VERBRAUCH = Decimal(str(ENERGY_VERBRAUCH))
我在事件中的键值对是
"energy_verbrauch": "2500000"
但我收到以下错误:
START RequestId: e2179e21-f225-44a1-bb99-266b184bc4e2 Version: $LATEST
unsupported operand type(s) for /: 'decimal.Decimal' and 'float'
END RequestId: e2179e21-f225-44a1-bb99-266b184bc4e2
和日志:
我查看了关于 SO 的一个已经存在的问题并遵循了该问题,但我仍然得到了错误。
问题:Python 3 Lambda 函数的输入 JSON 中的小数
然后ENERGY_VERBRAUCH
将其传递给函数
def factorize(energy_needed, df):
'''For scaling the Lastprofile on energy verbrauch'''
factor = energy_needed/(df['values'].sum()/1000)
df['values'] = (df['values']/1000)*factor
df['values'] = df['values'].round(3)
return df
解决方案
正如错误消息所说,Decimal
值不支持float
值除法。
我在事件中的键值对是
"energy_verbrauch": "2500000"
这是一个整数值。在这种情况下,使用Decimal
似乎没有必要。
你可以改变
from decimal import Decimal ENERGY_VERBRAUCH = event['energy_verbrauch'] ENERGY_VERBRAUCH = Decimal(str(ENERGY_VERBRAUCH))
至
ENERGY_VERBRAUCH = int(event['energy_verbrauch'])
(您也不需要使用str
,因为该值已经是一个字符串。)
然后将在 anint
和 a之间进行除法float
,这是允许的,并且会产生另一个float
。
推荐阅读
- php - 使用 htmlspecialchars() 在 PHP 中回显干净的文本
- android - 使用 OneTimeWorkRequest 和 enqueueUniqueWork 方法时多次调用 createWork
- apostrophe-cms - 管理栏中的撇号表单提交
- azure - Azure DevOps 在 Microsoft 托管代理上运行一项作业时释放管道“操作冲突错误”
- c# - 使用 System.Runtime.Serialization 但未找到 IgnoreDataMemberAttribute 和 DataMemberAttribute
- django - 如何将依赖项从一个应用程序注入到另一个应用程序的 Django 类中?
- java - HTTPClient JSON 的打印键
- reactjs - 我正在从 api 查询数据,但 useffect 呈现两次,所以我无法获取信息
- core-data - SwiftUI 2.0 / CoreData 2021:没有错误但实体未保存
- java - 是否响应实体
> 将所有内容存储在内存中?