在Python中,如何通过删除括号和大括号来打印Json
我想以一种好的方式打印Json,我想去掉括号、引号和大括号,只使用缩进和行尾来显示Json的结构 例如,如果我有这样一个Json:在Python中,如何通过删除括号和大括号来打印Json,python,json,dictionary,Python,Json,Dictionary,我想以一种好的方式打印Json,我想去掉括号、引号和大括号,只使用缩进和行尾来显示Json的结构 例如,如果我有这样一个Json: { "A": { "A1": 1, "A2": 2 }, "B": { "B1": { "B11": { "B111": 1,
{
"A": {
"A1": 1,
"A2": 2
},
"B": {
"B1": {
"B11": {
"B111": 1,
"B112": 2
},
"B12": {
"B121": 1,
"B122": 2
}
},
"B2": {
"B21": [1,2],
"B22": [1,2]
}
},
"C": "CC"
}
如何通过删除{}和[]来打印json,我想要的是:
A:
A1: 1
A2: 2
B:
B1:
B11:
B111: 1
B112: 2
B12:
B121: 1
B122: 2
B2:
B21: 1, 2
B22: 1, 2
C: CC
您可以将json加载到python对象中,然后将python对象转换为YAML。另一种解决方案是简单地迭代字典,并根据需要设置其格式 下面是一个将其转换为YAML的示例。它并没有给你想要的东西,但是它非常接近。有很多方法可以自定义输出,这只是一个简单的技巧,以展示总体思路:
import json
import yaml
data = json.loads('''
{
"A": {
"A1": 1,
"A2": 2
},
"B": {
"B1": {
"B11": {
"B111": 1,
"B112": 2
},
"B12": {
"B121": 1,
"B122": 2
}
},
"B2": {
"B21": [1,2],
"B22": [1,2]
}
},
"C": "CC"
}
''')
print yaml.safe_dump(data, allow_unicode=True, default_flow_style=False)
这是我得到的输出:
A:
A1: 1
A2: 2
B:
B1:
B11:
B111: 1
B112: 2
B12:
B121: 1
B122: 2
B2:
B21:
- 1
- 2
B22:
- 1
- 2
C: CC
如果您希望它采用最初指定的格式,您可以根据需要重载pyyaml类结构进行自定义: 代码:
import yaml
from yaml.emitter import Emitter
from yaml.serializer import Serializer
from yaml.representer import Representer
from yaml.resolver import Resolver
class MyRepresenter(Representer):
def represent_sequence(self, tag, sequence, flow_style=None):
value = []
node = yaml.SequenceNode(tag, value, flow_style=flow_style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
best_style = True
for item in sequence:
node_item = self.represent_data(item)
if not (isinstance(node_item, yaml.ScalarNode) and
not node_item.style):
best_style = False
value.append(node_item)
if best_style:
node = self.represent_data(
str(', '.join('%s' % x.value for x in value)))
if flow_style is None:
if self.default_flow_style is not None:
node.flow_style = self.default_flow_style
else:
node.flow_style = best_style
return node
class MyDumper(Emitter, Serializer, MyRepresenter, Resolver):
def __init__(self, stream,
default_style=None, default_flow_style=None,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None):
Emitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
Serializer.__init__(self, encoding=encoding,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
MyRepresenter.__init__(self, default_style=default_style,
default_flow_style=default_flow_style)
Resolver.__init__(self)
print yaml.dump(data, Dumper=MyDumper, default_flow_style=False)
A:
A1: 1
A2: 2
B:
B1:
B11:
B111: 1
B112: 2
B12:
B121: 1
B122: 2
B2:
B21: 1, 2
B22: 1, 2
C: CC
产生:
import yaml
from yaml.emitter import Emitter
from yaml.serializer import Serializer
from yaml.representer import Representer
from yaml.resolver import Resolver
class MyRepresenter(Representer):
def represent_sequence(self, tag, sequence, flow_style=None):
value = []
node = yaml.SequenceNode(tag, value, flow_style=flow_style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
best_style = True
for item in sequence:
node_item = self.represent_data(item)
if not (isinstance(node_item, yaml.ScalarNode) and
not node_item.style):
best_style = False
value.append(node_item)
if best_style:
node = self.represent_data(
str(', '.join('%s' % x.value for x in value)))
if flow_style is None:
if self.default_flow_style is not None:
node.flow_style = self.default_flow_style
else:
node.flow_style = best_style
return node
class MyDumper(Emitter, Serializer, MyRepresenter, Resolver):
def __init__(self, stream,
default_style=None, default_flow_style=None,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding=None, explicit_start=None, explicit_end=None,
version=None, tags=None):
Emitter.__init__(self, stream, canonical=canonical,
indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
Serializer.__init__(self, encoding=encoding,
explicit_start=explicit_start, explicit_end=explicit_end,
version=version, tags=tags)
MyRepresenter.__init__(self, default_style=default_style,
default_flow_style=default_flow_style)
Resolver.__init__(self)
print yaml.dump(data, Dumper=MyDumper, default_flow_style=False)
A:
A1: 1
A2: 2
B:
B1:
B11:
B111: 1
B112: 2
B12:
B121: 1
B122: 2
B2:
B21: 1, 2
B22: 1, 2
C: CC
那个输出格式看起来有点像,也许你们可以用它?谢谢Bryan,这就是我想要的。我还有一个问题,有没有办法将Json转换成yaml?我之所以这么问是因为将python对象转换为yaml会破坏顺序(dict中的键),但yaml似乎不支持OrderedDict。所以我在想,如果我能做到:python对象->Json,然后Json->yaml,以保持键的顺序。是的!非常感谢!