【2021-10-31】Python 字符串转字典格式

安装:pip install demjson
demjson.decode(string,encoding=‘utf-8’) # 把json字符串变json对象
demjson.encode(obj, encoding=‘utf-8’) # 把对象转换成json字符串


infos = [48,-126,1,34,48,13,6,9,42,-122,72,-122,-9,13,1,1,1,5,0,3,-126,1,15,0,48,-126,1,10,2,-126,1,1,0,-108,-13,-115,-97,34,-33,-77,31,12,-38,-58,-29,-68,126,22,8,80,-27,91,-38,117,-13,-31,-96,107,39,-54,-40,106,12,114,3,114,88,-113,111,-63,102,-79,48,-23,-75,-40,-74,9,-44,-88,58,-6,-122,-110,8,47,26,78,-6,2,46,-102,81,49,54,-84,-44,53,72,-39,80,27,-56,37,6,-88,-118,-21,-14,-22,63,79,-38,50,63,73,-33,-16,-93,49,-76,20,-123,-43,116,-22,27,-34,113,126,77,113,49,61,121,-125,-64,-63,70,-23,-59,44,-83,94,126,14,-122,-9,42,-4,46,-112,-62,9,40,-80,24,89,-19,68,-94,-8,-87,-120,-78,-10,92,78,-88,-109,122,-40,44,-98,-28,-116,87,52,-115,-63,-31,-21,7,-84,50,-38,-89,6,-75,40,31,95,-33,-69,-98,98,100,-48,97,86,-62,96,-78,-10,-120,51,15,-43,-40,-97,-6,-42,-9,-12,4,28,54,69,71,-55,46,-24,26,-100,22,-32,-64,-121,70,-16,-32,-63,14,-105,40,75,49,-74,-118,8,-17,-51,-99,52,-36,15,116,124,41,70,26,46,81,19,-26,100,-10,37,-18,-105,-104,-99,105,40,-44,108,77,-52,-23,-115,31,104,-61,35,-21,99,40,-77,-64,117,-39,-107,-60,35,28,-79,-3,-43,-105,2,3,1,0,1]

import re
import json
import demjson
data1  = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
text = demjson.decode(data1 ,encoding='utf-8')
print(type(text))
print(text)


data2 = """
{
    "serviceCode":"init",
    "idType":"CETP000001",
    "idCardNo":"110101199011017819",
    "productCode":"PDCD000012",
    "customerName":"王六",
    "pid":"cW89bXXz2RVJPV9F1RLVjQ==",
    "transactionId":"3746930761617",
    "applyType":"1"
}
"""
demjson_data = demjson.encode(data2, encoding='utf-8')   # 将数据转换为json 字符串,并可以指定编码格式
print(type(demjson_data))  # bytes
print(demjson_data)
'''
b'"\\n{\\n    \\"serviceCode\\":\\"init\\",\\n    \\"idType\\":\\"CETP000001\\",\\n    \\"idCardNo\\":\\"110101199011017819\\",\\n    \\"productCode\\":\\"PDCD000012\\",\\n    \\"customerName\\":\\"\xe7\x8e\x8b\xe5\x85\xad\\",\\n    \\"pid\\":\\"cW89bXXz2RVJPV9F1RLVjQ==\\",\\n    \\"transactionId\\":\\"3746930761617\\",\\n    \\"applyType\\":\\"1\\"\\n}\\n"'
'''
demjson_data2 = demjson.encode(demjson.decode(data2)).encode('utf-8').decode('unicode_escape')
print(type(demjson_data2))  # str
print(demjson_data2)
'''
{"applyType":"1","customerName":"王六","idCardNo":"110101199011017819","idType":"CETP000001","pid":"cW89bXXz2RVJPV9F1RLVjQ==","productCode":"PDCD000012","serviceCode":"init","transactionId":"3746930761617"}
'''
# json_data3 = json.loads(data2) # 这里没报错【是全部的键值对里面没有转义字符】
json_data3 = json.loads(demjson_data2)
print(type(json_data3))  # dict
print(json_data3)
'''
{'applyType': '1', 'customerName': '王六', 'idCardNo': '110101199011017819', 'idType': 'CETP000001', 'pid': 'cW89bXXz2RVJPV9F1RLVjQ==', 'productCode': 'PDCD000012', 'serviceCode': 'init', 'transactionId': '3746930761617'}
'''



print()
d_s =  """{'姓名': '张三', '性别': '男', '住址': "迷心岛'9号", '身份证号': '4302211', '民族': '汉'}"""
text2 = demjson.decode(d_s, encoding='utf-8')
print(type(text2)) # dict
print(text2)
'''
{'姓名': '张三', '性别': '男', '住址': "迷心岛'9号", '身份证号': '4302211', '民族': '汉'}
'''
text3 = demjson.encode(demjson.decode(d_s)).encode('utf-8').decode('unicode_escape')
print(type(text3)) # str
print(text3)
'''
{"住址":"迷心岛'9号","姓名":"张三","性别":"男","民族":"汉","身份证号":"4302211"}
'''
# text4 = json.loads(d_s) # 这里报错 json.decoder.JSONDecodeError 【因为这里的键值对有转义字符逗号: "迷心岛'9号"】
text4 = json.loads(text3)
print(type(text4))  # dict
print(text4)
'''
{'住址': "迷心岛'9号", '姓名': '张三', '性别': '男', '民族': '汉', '身份证号': '4302211'}
'''


print()
# 使用正则和demjson模块
s = """
{
    'customerId':'',
    'productTitle':"Art & Funclay" Length 8cm. 12 Sticks",
    'price': '135',
    'categoryId': 'test',
    'availability': 'test',
    'sku':'BPSIM00140',
    'departmentName': "test",
    'categoryName': "a",
    'subCategoryName': "test",
    'brandName':"test",
    'pageURL':"test"
}
"""
s = re.sub(r"'productTitle':\s*\"(.+?)\",", "'productTitle': '\\1',", s)
json_obj = demjson.decode(s, encoding='utf-8')
print(type(json_obj)) # dict
print(json_obj)
'''
{'customerId': '', 'productTitle': 'Art & Funclay" Length 8cm. 12 Sticks', 'price': '135', 'categoryId': 'test', 'availability': 'test', 'sku': 'BPSIM00140', 'departmentName': 'test', 'categoryName': 'a', 'subCategoryName': 'test', 'brandName': 'test', 'pageURL': 'test'}
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷心兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值