From 75a705df05d97e608606250d46a73b3970aea8b5 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Wed, 10 Mar 2021 16:08:02 -0500 Subject: [PATCH] =?UTF-8?q?Json2Yaml=20=E4=BD=BF=E7=94=A8=20Python=20?= =?UTF-8?q?=E5=B0=86=20Json=20=E6=95=B0=E6=8D=AE=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=20yaml=20=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Json2Yaml.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/Json2Yaml.py diff --git a/tests/Json2Yaml.py b/tests/Json2Yaml.py new file mode 100644 index 0000000..b16b4a7 --- /dev/null +++ b/tests/Json2Yaml.py @@ -0,0 +1,23 @@ +# Python script to Read file from JSON and convert to YAML +# Author - HoneyMoose (https://www.ossez.com) + +import json +import ruamel.yaml as yaml + +json_filename = 'resources/black_rock_test.json' +yaml_filename = 'resources/black_rock_test.yaml' + +# Read and process JSON +with open(json_filename) as json_file: + data = json.load(json_file) + print(type(data)) + + for data_dict in data: + label_id = data_dict['label_id'] + print(str(label_id)) +json_file.close() + +# Write to YAML +yaml_file = open(yaml_filename, 'w+') +yaml.dump(data, yaml_file, allow_unicode=True) +yaml_file.close()