python 的unicode类型的字符串最终转dict

a="[{u'category': u'flow', u'name': u'\u91d1\u5c91\u4e81', u'value': 10.14, u'createtime': u'2019-05-21 00:00:00', u'type': u'40', u'id': 890220}]"

形如 a的字符串最终转为list类型,转类型的时候各种报错,而且api接口各种数据不支持报错   "无效数据。期待为字典类型,得到的是 unicode 。"最终进行了如下转换才被接受

a="[{u'category': u'flow', u'name': u'\u91d1\u5c91\u4e81', u'value': 10.14, u'createtime': u'2019-05-21 00:00:00', u'type': u'40', u'id': 890220}]"
b1=str(a).decode('utf-8').replace('\'','\"').decode('unicode-escape').replace('u\"','\"')
print b1
print type(b1)
c1= json.loads(b1)
print c1
print type(c1)
d1=json.dumps(c1)
print d1
print type(d1)

打印输出:

[{"category": "flow", "name": "金岑亁", "value": 10.14, "createtime": "2019-05-21 00:00:00", "type": "40", "id": 890220}]
<type 'unicode'>
[{u'category': u'flow', u'name': u'\u91d1\u5c91\u4e81', u'value': 10.14, u'createtime': u'2019-05-21 00:00:00', u'type': u'40', u'id': 890220}]
<type 'list'>
[{"category": "flow", "name": "\u91d1\u5c91\u4e81", "value": 10.14, "createtime": "2019-05-21 00:00:00", "type": "40", "id": 890220}]
<type 'str'>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值