将字符串转换为字典python的列表

时间:2019-05-30 07:52:16

标签: python python-3.x string list dictionary

我是python的新手。我有一个字符串从我的sqlite数据库中获取。我想将字符串转换为list of list of dicts。我尝试了astjson库,但是失败了。

这是字符串:

a = '"[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]"'

这是我尝试的代码:

import ast
import json
a = '"[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]"'
a = a.replace("[[","[").replace("]]","]")
print(a)

# using json library- fails
# jdata = json.loads(a)
# for d in jdata:
#     for key, value in d.iteritems():
#         print (key, value)

#using ast library - it also fails
# res = [ast.literal_eval(x) for x in a]

我尝试了此链接convert string to dictconvert str to list of list

但是对于我来说,我有一个list of list of dicts,它是字符串形式。如何使其成为可能。

我希望与输出相同,但是它应该在字典列表中。

必需的输出:

[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]

3 个答案:

答案 0 :(得分:5)

这应该有效。

import ast
a = '"[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]"'

print(ast.literal_eval(a.strip('"')))

输出:

[[{'dbname': 'smackcoders',
   'host': 'localhost',
   'id': 'mysql1',
   'limit_count': 5,
   'password': 'root',
   'plugin': 'mysql',
   'plugin_type': 'input',
   'tbname': 'agg_csv',
   'user': 'root'},
  {'action': 'count',
   'field': 'state',
   'field_name': 'count_result',
   'id': 'metrics',
   'input_from': 'mysql1',
   'plugin': 'metrics',
   'plugin_type': 'filter',
   'send_data_immediately': True,
   'value': 'kerala'},
  {'doc_typ': 'sm23',
   'id': 'elastic_search',
   'ind': 'neww10',
   'input_from': 'metrics',
   'plugin': 'elastic',
   'plugin_type': 'output'}]]

答案 1 :(得分:4)

可以使用COPY ./package.yaml /app/package.yaml # Copying the package file RUN stack build --only-dependencies # Build dependencies only COPY . /app # Copying the rest of the files EXPOSE 3000 CMD ["stack", "build", "--exec", "APP_NAME"] # Build the application itself 。您只需要确保您输入的字符串可以被解析即可。在这种情况下,只需删除开头和结尾的引号即可:

ast.literal_eval

答案 2 :(得分:4)

尝试一下。

a = '"[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]"'

a=a.strip('"')
a=eval(a)