python-tutorials/tests/FileWith.py

26 lines
633 B
Python
Raw Normal View History

2021-03-11 12:30:23 -05:00
# -*- coding: utf-8 -*-
2021-03-11 10:40:37 -05:00
# Python script to Read file from JSON and convert to YAML
# Author - HoneyMoose (https://www.ossez.com)
import json
2021-03-11 10:51:43 -05:00
import ruamel.yaml as yaml
2021-03-11 10:40:37 -05:00
json_filename = 'resources/honeymoose_test.json'
2021-03-11 10:51:43 -05:00
yaml_filename = 'resources/honeymoose_test.yaml'
2021-03-11 10:40:37 -05:00
# 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))
print(json_file.closed)
2021-03-11 10:51:43 -05:00
# Using with write to YAML
with open(yaml_filename, 'w') as yaml_file:
yaml.dump(data, yaml_file, allow_unicode=True)