【问题标题】:How to convert python string data to list or in dict?如何将python字符串数据转换为列表或字典?
【发布时间】:2013-10-11 21:50:11
【问题描述】:

这是我来自 GCM-python 的响应代码,

{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b
9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"erro
r":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"er
ror":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865
596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}

当我收到此响应时,我想从字典中收集所有错误键...但它似乎是一个字符串,我将尝试使用 json.dumps() 在 json 中转储然后删除斜线但对我不起作用,甚至 ast 都不起作用。我试试这个python json dumps。我在那里想念什么?请帮助我。

【问题讨论】:

  • 如果确实是字符串,使用json.loads而不是json.dumps。 Dump = 从数据对象到字符串(序列化); load = 从字符串到数据对象(反序列化)。
  • 我尝试使用 json.loads...比如在变量中存储响应并在循环中迭代:coderesult = [ json.loads(resp) for resp in data ]code and i得到一个错误:codeValueError: 未终止的字符串开始于:第 1 行第 104 列 (char 104)code
  • 阅读你的问题,尤其是阅读你的 cmets 给的答案,如果你甚至不知道你的输入是字符串还是其他东西,真的会限制任何人帮助你的能力。
  • 我知道我在问什么,现在我将我的问题解释到我被卡住的地步......你也可以在代码中显示,输出是字符串并且无法在列表中转换,如果我走错路,请原谅我

标签: python json list dictionary


【解决方案1】:

如果是字符串,加载它,不要转储它:

#! /usr/bin/python3

import json

a = '''{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}'''

j = json.loads(a)
errors = [d for d in j ['results'] if 'error' in d]
print(errors)

【讨论】:

  • 最初我尝试加载它,但它显示一个错误:ValueError: Unterminated string started at: line 1 column 104 (char 104),所以首先我转储它然后加载它,因为 json 给了我一个错误。
  • 当 python GCM 返回一个响应对象时我得到了响应,然后我迭代并将数据存储在一个列表中,或者尝试从 json.loads 循环中创建一个列表但没有工作
【解决方案2】:

由于您收到的数据是有效的 Python 数据,您可以简单地使用 [ast.literal_eval][1]

演示

import ast
data = '''{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}'''

>>> pp.pprint(ast.literal_eval(data))
{   'canonical_ids': 0,
    'failure': 15,
    'multicast_id': 6343554431392278573L,
    'results': [   {   'message_id': '0:1380910865603840%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865592683%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865600910%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865596592%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865595499%356b9054f9fd7ecd'}],
    'success': 5}
>>> 

随后转储错误

>>> pp.pprint([elem['error'] for elem in ast.literal_eval(data)['results'] if 'error' in elem])
[   'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered']

【讨论】:

  • 它看起来非常棒的答案,但我遇到了一些问题,这就是我要做的:GCM 给了我包含 http 状态代码的响应对象,当我像这样迭代它时:data = for resp in response print resp,然后它显示包含字符串的字典。现在我尝试应用你的解决方案,但它给了我错误“SyntaxError:扫描字符串文字时 EOL”,它使我的响应在字符串块中变得混乱..
  • 嘿感谢这个解决方案,实际上我的数据不是正确的字符串......所以首先我将它设为字符串 d = (''.join('' + resp + '' for响应 g_response)) 然后应用 ast...
猜你喜欢
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多