diff --git a/_sidebar.md b/_sidebar.md index e2fa30e..7693cac 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -9,8 +9,8 @@ - [第一个 Hello World 程序](docs/course/foundation/04_hello_world.md) - [语言基础](docs/course/foundation/05_language_fundamentals.md) -- 其他小工具 - - [JWT](jwt/README.md) +- 附录索引 + - [Python 用例索引](docs/appendix/01_use_cases.md) - [MessagePack](message-pack/index.md) - [Protocol Buffers](protocol-buffers/index.md) diff --git a/tests/FileWith.py b/tests/FileWith.py index b989bb5..221189c 100644 --- a/tests/FileWith.py +++ b/tests/FileWith.py @@ -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) diff --git a/tests/Json2Yaml.py b/tests/Json2Yaml.py index b16b4a7..b13090a 100644 --- a/tests/Json2Yaml.py +++ b/tests/Json2Yaml.py @@ -2,6 +2,7 @@ # Author - HoneyMoose (https://www.ossez.com) import json + import ruamel.yaml as yaml json_filename = 'resources/black_rock_test.json' @@ -18,6 +19,5 @@ with open(json_filename) as json_file: json_file.close() # Write to YAML -yaml_file = open(yaml_filename, 'w+') -yaml.dump(data, yaml_file, allow_unicode=True) -yaml_file.close() +with open(yaml_filename, 'w') as yaml_file: + yaml.dump(data, yaml_file, allow_unicode=True)