gpt4 book ai didi

Python 字典作为文本存储在 MySQL 中

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:42 25 4
gpt4 key购买 nike

我在 MySQL 数据库中有一列,Python 字典以文本形式存储。

输入:

print(row.optional_items)
print(type(optional_items))

输出:

{'Bed 90,-': '90,00', 'Baby bath': '90,00', 'Child chair': '90,00', 'Fee': '150,00', 'End cleaning'
: '730,00', 'Linned Kr. 90,-': '90,00'}
<class 'str'>

我想将变量作为 Python 字典来使用,但这是不可能的,因为 Python 认为它是一个字符串。我尝试使用 dict(row.feline_optional_items) 将其转换为字典,但它返回 ValueError: dictionary update sequence element #0 has length 1; 2 是必需的

有没有办法将 row.optional_items 转换为字典以便我可以使用它?

我希望有人能提供帮助。谢谢!

最佳答案

如果你想将字符串形式的字典转换为实际的字典,请执行以下操作:

from ast import literal_eval
dict_str = "{'a': 1}"
dict_dict = literal_eval(dict_str)

关于Python 字典作为文本存储在 MySQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40751462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com