ReK*_*eKx 11 python json yaml python-3.x
如何加载YAML文件并将其转换为Python JSON对象?
我的YAML文件如下所示:
Section:
heading: Heading 1
font:
name: Times New Roman
size: 22
color_theme: ACCENT_2
SubSection:
heading: Heading 3
font:
name: Times New Roman
size: 15
color_theme: ACCENT_2
Paragraph:
font:
name: Times New Roman
size: 11
color_theme: ACCENT_2
Table:
style: MediumGrid3-Accent2
Run Code Online (Sandbox Code Playgroud)
Bea*_*own 10
您可以使用 PyYAML
pip install PyYAML
Run Code Online (Sandbox Code Playgroud)
并在ipython控制台中:
In [1]: import yaml
In [2]: document = """Section:
...: heading: Heading 1
...: font:
...: name: Times New Roman
...: size: 22
...: color_theme: ACCENT_2
...:
...: SubSection:
...: heading: Heading 3
...: font:
...: name: Times New Roman
...: size: 15
...: color_theme: ACCENT_2
...: Paragraph:
...: font:
...: name: Times New Roman
...: size: 11
...: color_theme: ACCENT_2
...: Table:
...: style: MediumGrid3-Accent2"""
...:
In [3]: yaml.load(document)
Out[3]:
{'Paragraph': {'font': {'color_theme': 'ACCENT_2',
'name': 'Times New Roman',
'size': 11}},
'Section': {'font': {'color_theme': 'ACCENT_2',
'name': 'Times New Roman',
'size': 22},
'heading': 'Heading 1'},
'SubSection': {'font': {'color_theme': 'ACCENT_2',
'name': 'Times New Roman',
'size': 15},
'heading': 'Heading 3'},
'Table': {'style': 'MediumGrid3-Accent2'}}
Run Code Online (Sandbox Code Playgroud)
Ant*_*hon 10
没有 Python JSON 对象这样的东西。JSON 是一种独立于语言的文件格式,其根源在于 JavaScript,并且受到许多语言的支持。
如果您的 YAML 文档遵循旧的 1.1 标准,即 2009 之前的标准,您可以按照其他一些答案的建议使用 PyYAML。
如果它使用较新的 YAML 1.2 规范,该规范使 YAML 成为 JSON 的超集,则您应该使用ruamel.yaml(免责声明:我是该包的作者,它是 PyYAML 的一个分支)。
import ruamel.yaml
import json
in_file = 'input.yaml'
out_file = 'output.json'
yaml = ruamel.yaml.YAML(typ='safe')
with open(in_file) as fpi:
data = yaml.load(fpi)
with open(out_file, 'w') as fpo:
json.dump(data, fpo, indent=2)
Run Code Online (Sandbox Code Playgroud)
产生output.json:
{
"Section": {
"heading": "Heading 1",
"font": {
"name": "Times New Roman",
"size": 22,
"color_theme": "ACCENT_2"
}
},
"SubSection": {
"heading": "Heading 3",
"font": {
"name": "Times New Roman",
"size": 15,
"color_theme": "ACCENT_2"
}
},
"Paragraph": {
"font": {
"name": "Times New Roman",
"size": 11,
"color_theme": "ACCENT_2"
}
},
"Table": {
"style": "MediumGrid3-Accent2"
}
}
Run Code Online (Sandbox Code Playgroud)
ruamel.yaml除了支持 YAML 1.2 之外,还修复了许多 PyYAML 错误。您还应该注意load(),如果您无法始终完全控制输入,则PyYAML也被记录为不安全的。PyYAML还负载标数字021为整数17,而不是21和标串等转换on,yes,off布尔值(分别是True,True和False)。
PyYAML库用于此目的
pip install pyyaml
Run Code Online (Sandbox Code Playgroud)
import yaml
import json
with open("example.yaml", 'r') as yaml_in, open("example.json", "w") as json_out:
yaml_object = yaml.safe_load(yaml_in) # yaml_object will be a list or a dict
json.dump(yaml_object, json_out)
Run Code Online (Sandbox Code Playgroud)
注意:PyYAML仅支持2009之前的YAML 1.1规范。
如果需要YAML 1.2,则可以选择ruamel.yaml。
pip install ruamel.yaml
Run Code Online (Sandbox Code Playgroud)
小智 5
在 python3 中,您可以使用pyyaml。
$ pip3 install pyyaml
Run Code Online (Sandbox Code Playgroud)
然后加载 yaml 文件并将其转储为 json:
import yaml, json
with open('./file.yaml') as f:
print(json.dumps(yaml.load(f)))
Run Code Online (Sandbox Code Playgroud)
输出:
{"Section": null, "heading": "Heading 1", "font": {"name": "Times New Roman", "size": 22, "color_theme": "ACCENT_2"}, "SubSection": {"heading": "Heading 3", "font": {"name": "Times New Roman", "size": 15, "color_theme": "ACCENT_2"}}, "Paragraph": {"font": {"name": "Times New Roman", "size": 11, "color_theme": "ACCENT_2"}}, "Table": {"style": "MediumGrid3-Accent2"}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10673 次 |
| 最近记录: |