Python:json.loads 返回以“u"为前缀的项目 [英] Python: json.loads returns items prefixing with 'u'

查看:93
本文介绍了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"为前缀的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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