Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字典作为文本存储在MySQL中_Python_Mysql_Sql_Dictionary - Fatal编程技术网

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

Python字典作为文本存储在MySQL中,python,mysql,sql,dictionary,Python,Mysql,Sql,Dictionary,我在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'

我在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'>
{'Bed 90,':'90,00','Baby bath':'90,00','Child chair':'90,00','Fee':'150,00','End cleaning'
:'730,00','Linned Kr.90,-':'90,00'}
我希望将该变量用作Python字典,但这是不可能的,因为Python将其视为字符串。我尝试使用
dict(row.felline\u可选项)
将其转换为dict,但它返回
ValueError:dictionary update sequence元素#0的长度为1;2是必需的

有没有办法将row.optional_项转换为字典,以便我可以使用它


我希望有人能帮忙。谢谢

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

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

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

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

您应该强调使用literal_eval()而不是eval(),因为eval()是危险的。谢谢!工作起来很有魅力!您应该强调使用literal_eval()而不是eval(),因为eval()是危险的。谢谢!工作起来很有魅力!如果您可以控制列中存储的内容,那么最好将这些数据存储为JSON,因为这是一种标准格式,其他语言也可以轻松读取数据。甚至最好规范化数据库,并将每个字典元素存储在单独的行中,使用属性值表。如果您可以控制列中存储的内容,则最好将此数据存储为JSON,因为它是一种标准格式,其他语言也可以轻松读取数据。最好规范化数据库,并将每个字典元素存储在单独的行中,使用属性值表。