【2021-11-04】Python 字符串转字典格式(第二版)

pip install hjson -i https://pypi.tuna.tsinghua.edu.cn/simple

import re
import json
import hjson
import demjson



def compile_demjson(text):
	text = demjson.decode(text ,encoding='utf-8')
	print(type(text),text)


def compile_hjson(text):
	obj = hjson.loads(text)
	print(type(obj), obj)
	result = hjson.dumpsJSON(obj)
	print(type(result),result)
	result = json.loads(result)
	print(type(result),result)

示例一

data1  = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
compile_demjson(data1)
compile_hjson(data1)

输出:
<class 'dict'> {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
<class 'collections.OrderedDict'> OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
<class 'str'> {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
<class 'dict'> {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
[Finished in 1.0s]

示例二

data2 = """
{
    "serviceCode":"init",
    "idType":"CETP000001",
    "idCardNo":"110101199011017819",
    "productCode":"PDCD000012",
    "customerName":"王六",
    "pid":"cW89bXXz2RVJPV9F1RLVjQ==",
    "transactionId":"3746930761617",
    "applyType":"1"
}
"""
compile_demjson(data2)
compile_hjson(data2)


输出:
<class 'dict'> {'serviceCode': 'init', 'idType': 'CETP000001', 'idCardNo': '110101199011017819', 'productCode': 'PDCD000012', 'customerName': '王六', 'pid': 'cW89bXXz2RVJPV9F1RLVjQ==', 'transactionId': '3746930761617', 'applyType': '1'}
<class 'collections.OrderedDict'> OrderedDict([('serviceCode', 'init'), ('idType', 'CETP000001'), ('idCardNo', '110101199011017819'), ('productCode', 'PDCD000012'), ('customerName', '王六'), ('pid', 'cW89bXXz2RVJPV9F1RLVjQ=='), ('transactionId', '3746930761617'), ('applyType', '1')])
<class 'str'> {"serviceCode": "init", "idType": "CETP000001", "idCardNo": "110101199011017819", "productCode": "PDCD000012", "customerName": "\u738b\u516d", "pid": "cW89bXXz2RVJPV9F1RLVjQ==", "transactionId": "3746930761617", "applyType": "1"}
<class 'dict'> {'serviceCode': 'init', 'idType': 'CETP000001', 'idCardNo': '110101199011017819', 'productCode': 'PDCD000012', 'customerName': '王六', 'pid': 'cW89bXXz2RVJPV9F1RLVjQ==', 'transactionId': '3746930761617', 'applyType': '1'}
[Finished in 1.3s]

示例三

data3 =  """{'姓名': '张三', '性别': '男', '住址': "迷心岛'9号", '身份证号': '4302211', '民族': '汉'}"""
compile_demjson(data3)
compile_hjson(data3)


输出:
<class 'dict'> {'姓名': '张三', '性别': '男', '住址': "迷心岛'9号", '身份证号': '4302211', '民族': '汉'}
<class 'collections.OrderedDict'> OrderedDict([('姓名', '张三'), ('性别', '男'), ('住址', "迷心岛'9号"), ('身份证号', '4302211'), ('民族', '汉')])
<class 'str'> {"\u59d3\u540d": "\u5f20\u4e09", "\u6027\u522b": "\u7537", "\u4f4f\u5740": "\u8ff7\u5fc3\u5c9b'9\u53f7", "\u8eab\u4efd\u8bc1\u53f7": "4302211", "\u6c11\u65cf": "\u6c49"}
<class 'dict'> {'姓名': '张三', '性别': '男', '住址': "迷心岛'9号", '身份证号': '4302211', '民族': '汉'}
[Finished in 1.2s]

示例四

data4 = """
{
    '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"
}
"""
data4 = re.sub(r"'productTitle':\s*\"(.+?)\",", "'productTitle': '\\1',", data4)
compile_demjson(data4)
compile_hjson(data4)


输出:
<class 'dict'> {'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'}
<class 'collections.OrderedDict'> OrderedDict([('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')])
<class 'str'> {"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"}
<class 'dict'> {'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'}
[Finished in 1.3s]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

迷心兔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值