马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 鱼cpython学习者 于 2022-8-6 21:34 编辑
自己实现的json格式化
数据a = {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}
代码def format_json(array, level=0):
result = ''
if isinstance(array, dict):
if not array:
result += '{}'
result += '\n'
for key, value in array.items():
result += '\t' * level + key + ": " + format_json(value, level + 1)
return result
elif isinstance(array, list):
return format_json({f'[{index}]': item for index, item in enumerate(array)}, level)
else:
return result + repr(array) + '\n'
输出glossary:
title: 'example glossary'
GlossDiv:
title: 'S'
GlossList:
GlossEntry:
ID: 'SGML'
SortAs: 'SGML'
GlossTerm: 'Standard Generalized Markup Language'
Acronym: 'SGML'
Abbrev: 'ISO 8879:1986'
GlossDef:
para: 'A meta-markup language, used to create markup languages such as DocBook.'
GlossSeeAlso:
[0]: 'GML'
[1]: 'XML'
GlossSee: 'markup'
使用line_profile分析性能时间为0.0003052 sTimer unit: 1e-06 s
Total time: 0.0003052 s
File: .\json_formater.py
Function: format_json at line 6
Line # Hits Time Per Hit % Time Line Contents
==============================================================
6 @profile
7 def format_json(array, level=0):
8 19 15.9 0.8 5.2 result = ''
9 19 164.0 8.6 53.7 if isinstance(array, dict):
10 7 6.0 0.9 2.0 if not array:
11 result += '{}'
12 7 5.6 0.8 1.8 result += '\n'
13 24 27.9 1.2 9.1 for key, value in array.items():
14 17 44.2 2.6 14.5 result += '\t' * level + key + ": " + format_json(value, level + 1)
15 7 5.2 0.7 1.7 return result
16 12 10.8 0.9 3.5 elif isinstance(array, list):
17 1 8.2 8.2 2.7 return format_json({f'[{index}]': item for index, item in enumerate(array)}, level)
18 else:
19 11 17.4 1.6 5.7 return result + repr(array) + '\n'
加上输出变为 0.0008409 sTimer unit: 1e-06 s
Total time: 0.0008409 s
File: .\json_formater.py
Function: printj at line 22
Line # Hits Time Per Hit % Time Line Contents
==============================================================
22 @profile
23 def test():
24 1 840.9 840.9 100.0 print(format_json(a))
使用python标准库json实现的效果:print(json.dumps(a, sort_keys=True, indent=4, separators=(',', ': ')))
{
"glossary": {
"GlossDiv": {
"GlossList": {
"GlossEntry": {
"Abbrev": "ISO 8879:1986",
"Acronym": "SGML",
"GlossDef": {
"GlossSeeAlso": [
"GML",
"XML"
],
"para": "A meta-markup language, used to create markup languages such as DocBook."
},
"GlossSee": "markup",
"GlossTerm": "Standard Generalized Markup Language",
"ID": "SGML",
"SortAs": "SGML"
}
},
"title": "S"
},
"title": "example glossary"
}
}
line_profile分析0.0014262 sTimer unit: 1e-06 s
Total time: 0.0014262 s
File: .\json_formater.py
Function: printj at line 22
Line # Hits Time Per Hit % Time Line Contents
==============================================================
22 @profile
23 def test():
24 1 1426.2 1426.2 100.0 print(json.dumps(a, sort_keys=True, indent=4, separators=(',', ': ')))
虽然快不了多少,但对于我这个菜鸡来说还是值得庆祝的 |