将字符串转换为字典Python 3 [英] Convert String to List of Dictionaries Python 3

查看:87
本文介绍了将字符串转换为字典Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条类似的信息:

[{"city": "Beverly Hills", "state": "", "postal_code": "", "address": "Some Address", "country": "USA"}, {"city": "New York", "state": "NY", "postal_code": "", "address": "P.O. BOX 52404", "country": "USA"}]

当我执行type()时,它显示为<class 'str'>.

When I do type() it shows as <class 'str'>.

如何从Python 3中的字符串到字典列表中获取此信息?

How do I get this information from a string to a list of dictionaries in Python 3?

我尝试了literal_eval并收到了错误malformed node or string:,所以我不确定执行此操作的最佳方法是什么.'

I've tried literal_eval and got an error malformed node or string:, so I am not sure what the best way to do this is.'

编辑

以下是一个可以重现的示例:

Here is an example that should be reproducible:

mydata = {'programs': '["France"]', 'ids': '[]', 'citizenships': '[]', 'nationalities': '["FR"]', 'places_of_birth': '[]', 'dates_of_birth': '["1973-03-25"]', 'addresses': '[{"state": null, "postal_code": null, "address": null, "city": null, "country": "FR"}]'}
for key,value in mydata.items():
    if type(value) is str:
        result = literal_eval(value)
        print("value 1: ", value)
        print("value type 2:", type(value))
        print("result 3: ", result)
        print("result  4: ", type(result))
        for item in result:
            print("item in result 5:", item)
            print("type of item in result 6:", type(item))

这是错误:

insert_in_db中的文件"server.py",第137行 结果= literal_eval(值)
文件"/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py",第84行,位于literal_eval中 返回_convert(node_or_string)
_convert中的文件"/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py",第57行 返回列表(map(_convert,node.elts))
_convert中的文件"/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py",第62行 在zip中(node.keys,node.values))
在第61行的"/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py"文件中 返回dict((__ convert(k),_convert(v))表示k,v
_convert中的文件"/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py",第83行 引发ValueError('格式错误的节点或字符串:'+ repr(node)) ValueError:格式错误的节点或字符串:< _ast.Name对象位于0x109baae48>

File "server.py", line 137, in insert_in_db result = literal_eval(value)
File "/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py", line 84, in literal_eval return _convert(node_or_string)
File "/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py", line 57, in _convert return list(map(_convert, node.elts))
File "/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py", line 62, in _convert in zip(node.keys, node.values))
File "/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py", line 61, in return dict((_convert(k), _convert(v)) for k, v
File "/Users/user/anaconda3/envs/apicaller/lib/python3.5/ast.py", line 83, in _convert raise ValueError('malformed node or string: ' + repr(node)) ValueError: malformed node or string: <_ast.Name object at 0x109baae48>

也许我错过了检查空值的步骤?我似乎在eval line 137上收到错误.我从下面提到的堆栈溢出注释中使用ast.literal_eval的想法.

Maybe I am missing a step in between to check for null values? I seem to get the error on the eval line 137. I got the idea to use ast.literal_eval from that stack overflow comment mentioned below.

它比我处理数据的方式更多是数据问题吗?我对Python不太熟悉,因此很可能会缺少一些东西.

Is it more of a data issue than with the way I am handling it? I am not very familiar with Python so I am most likely missing something.

推荐答案

import json

data = json.loads(<your_string>)

这篇关于将字符串转换为字典Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆