本文介绍了Python:json.loads返回以"u"为前缀的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将以Obj-C的形式接收JSON编码的字符串,并且正在解码一个伪字符串(目前),如下面的代码所示.我的输出结果是在每个项目前面加上字符"u":
I'll be receiving a JSON encoded string form Obj-C, and I am decoding a dummy string (for now) like the code below. My output comes out with character 'u' prefixing each item:
[{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}...
JSON如何添加此Unicode字符?删除它的最佳方法是什么?
How is JSON adding this unicode char? What's the best way to remove it?
mail_accounts = []
da = {}
try:
s = '[{"i":"imap.gmail.com","p":"aaaa"},{"i":"imap.aol.com","p":"bbbb"},{"i":"333imap.com","p":"ccccc"},{"i":"444ap.gmail.com","p":"ddddd"},{"i":"555imap.gmail.com","p":"eee"}]'
jdata = json.loads(s)
for d in jdata:
for key, value in d.iteritems():
if key not in da:
da[key] = value
else:
da = {}
da[key] = value
mail_accounts.append(da)
except Exception, err:
sys.stderr.write('Exception Error: %s' % str(err))
print mail_accounts
推荐答案
u-前缀仅表示您具有Unicode字符串.当您真正使用字符串时,它不会出现在您的数据中.不要被打印输出扔掉.
The u- prefix just means that you have a Unicode string. When you really use the string, it won't appear in your data. Don't be thrown by the printed output.
例如,尝试以下操作:
print mail_accounts[0]["i"]
你不会看到你.
这篇关于Python:json.loads返回以"u"为前缀的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!