Python:"unicode"对象没有属性"iteritems" [英] Python: 'unicode' object has no attribute 'iteritems'

查看:78
本文介绍了Python:"unicode"对象没有属性"iteritems"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Python 2.6.9的应用中,我将传入的JSON作为unicode字符串:

In my app using Python 2.6.9 I have this incoming JSON as a unicode string:

{"devices": "{1540702298: u\"{'on': u'True', 'group': '2', 'time': u'2015-06-04 16:37:52', 'value': u'74.1', 'lastChange': u'2015-06-05 09:28:10'}\"}"}

我已经尝试了多种方法来解析它,但是仍然出现相同的错误...

I have tried in various ways to parse it but I still get the same error...

例如:

a = unicode({"devices": "{1540702298: u\"{'on': u'True', 'group': '2', 'energyAccumBaseTime': u'2015-06-04 16:37:52', 'sensorValueRaw': u'74.1', 'lastChange': u'2015-06-05 09:28:10', 'energyAccumTotal': u'1.3', 'sensorValueUi': u'74.1'}\"}"})

b = json.loads(a)

print b['devices'] # all good I get the contents...

但是当我这样做时...

but when I do...

for k, v in b['devices'].iteritems():

print k

我得到了错误:

"unicode"对象没有属性"iteritems"

'unicode' object has no attribute 'iteritems'

如何完整解析传入的JSON?

How can I parse this incoming JSON in full?

推荐答案

外部对象可能是JSON,但是它包含一个字符串,该字符串本身就是Python字典文字,其中包含Unicode文字作为值.

The outer object may be JSON, but it contains a string that itself is a Python dictionary literal, containing Unicode literals as values.

有人这样做了:

python_dictionary = {}
python_dictionary[integer_key] = str(another_dictionary)
outer_object {"devices": str(python_dictionary)}

,然后将outer_object编码为JSON.该词典中的每个值本身就是代表Python词典的另一个字符串,并且这些词典包含更多表示词典的字符串,例如许多俄罗斯套娃.

before encoding the outer_object to JSON. Each value in that dictionary is itself another string representing a Python dictionary, and those dictionaries contain more strings representing dictionaries, like so many Matryoshka dolls.

您可以使用 ast.literal_eval()函数来打开返回到Python对象:

You can use the ast.literal_eval() function to turn that back into Python objects:

import ast
import json

yourobject = json.loads(jsondata)
dictionary = ast.literal_eval(yourobject['devices'])
for key, nested in dictionary.iteritems():
    nested = ast.literal_eval(nested)
    print key, nested

,但是您应该真正修复产生字符串的任何内容,以避免将嵌套字典存储为字符串.请注意,该嵌套字典中的键是整数;那些必须转换为字符串才能在JSON中工作.

but you should really fix whatever produced the string to avoid storing the nested dictionary as a string. Note that the keys in that nested dictionary are integers; those would have to be converted to strings to work in JSON.

演示:

>>> import json
>>> import ast
>>> jsondata = r'''{"devices": "{1540702298: u\"{'on': u'True', 'group': '2', 'time': u'2015-06-04 16:37:52', 'value': u'74.1', 'lastChange': u'2015-06-05 09:28:10'}\"}"}'''
>>> yourobject = json.loads(jsondata)
>>> type(yourobject['devices'])
<type 'unicode'>
>>> ast.literal_eval(yourobject['devices'])
{1540702298: u"{'on': u'True', 'group': '2', 'time': u'2015-06-04 16:37:52', 'value': u'74.1', 'lastChange': u'2015-06-05 09:28:10'}"}
>>> dictionary = ast.literal_eval(yourobject['devices'])
>>> dictionary[1540702298]
u"{'on': u'True', 'group': '2', 'time': u'2015-06-04 16:37:52', 'value': u'74.1', 'lastChange': u'2015-06-05 09:28:10'}"
>>> type(dictionary[1540702298])
<type 'unicode'>

>>> for key, nested in dictionary.iteritems():
...     nested = ast.literal_eval(nested)
...     print key, nested
... 
1540702298 {'on': u'True', 'lastChange': u'2015-06-05 09:28:10', 'group': '2', 'value': u'74.1', 'time': u'2015-06-04 16:37:52'}

这篇关于Python:"unicode"对象没有属性"iteritems"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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