使用 with 语句写入 yaml 文件

This commit is contained in:
YuCheng Hu 2021-03-11 10:51:43 -05:00
parent 8800a0f19b
commit b0ee521470
No known key found for this signature in database
GPG Key ID: 1E5CBEF8B550FB7D
1 changed files with 7 additions and 0 deletions

View File

@ -3,7 +3,10 @@
import json
import ruamel.yaml as yaml
json_filename = 'resources/honeymoose_test.json'
yaml_filename = 'resources/honeymoose_test.yaml'
# Read and process JSON
with open(json_filename) as json_file:
@ -15,3 +18,7 @@ with open(json_filename) as json_file:
print(str(label_id))
print(json_file.closed)
# Using with write to YAML
with open(yaml_filename, 'w') as yaml_file:
yaml.dump(data, yaml_file, allow_unicode=True)