TypeError:无法散列的类型:'dict'Python / Flask

时间:2020-02-04 09:05:55

标签: python

因此,我正在开发一个小程序,该程序应该将值发送到firestore database,几乎所有功能都可以正常工作,但是我从这部分代码中得到了错误。我正在尝试保存string内的temp

 if block == "ITEMS":
        champs = form.areaItems.data #Get the user input text field from the WTForm (it's a dict for whatever reason)
        itemsChamps = ItemsChamps(champs.values()) #Stock the dict value inside itemsChamps 
        temp = next(iter(itemsChamps.name)) #Get the 1st value from itemsChamps (I only want the 1st value)
        data = {
            "items": {
                champs: {
                    "string": temp
                }
            }
        }

这是错误:

File "C:\[..]\flaskblog\routes.py", line 63, in ajouter

"string": temp

TypeError: unhashable type: 'dict'

我的代码可能看起来有点“”“令人困惑”“”,我是新手,对此表示抱歉!

编辑1:现在可以使用!

我现在感觉很傻,我对自己编写的所有代码感到困惑,有一些错误:

        if block == "ITEMS":
        champs = form.itemsFields.data #I was using the wrong form field...
        itemsChamps = ItemsChamps(form.areaItems.data.values()) #I'm now getting all the value from the right field
        temp = next(iter(itemsChamps.name)) #Didn't touch this, it work
        data = {
            "items": {
                champs: {
                    "string": temp
                }
            }
        }

感谢您给我一些时间!

2 个答案:

答案 0 :(得分:1)

您正在尝试使用dict作为dict键,以引用您的评论:“出于某种原因是dict”。字典不可散列,因此不能用作键。也许从字典中提取数据并将其用作键?

答案 1 :(得分:1)

问题在于这段代码。 df1=pd.DataFrame([dict(y.split("=") for y in x.strip("' ").split(",")) for x in df['Column']], index=df.index) print (df1) ID Name Type Value Registration Info: Sn \ 0 1 GDNDL380F4 FTT-87 GDNDL380F4 GDNDL380F4 1 1 GDNDL4040F4 FTT-87 GDNDL4040F4 GDNDL380F4 2 1 GDNDL380F5 FTT-87 GDNDL380F5 GDNDL380F4 Service Level 0 Service Level Disabled 1 Service Level Disabled 2 Service Level Disabled 是一个字典,您将它用作键,dict键必须是str,int,float(通常可以被哈希处理而不是字典)

champs

如果您感兴趣的数据是data = { "items": { champs: { "string": temp } } } ,可以将champs["user_input"]更改为champs来解决此问题。