[英]How to append new key and value to a yaml dictionary file
如何將一對新的鍵和值附加到 yaml 文件,而不刪除文件內容或將整個 yaml 重新添加到自身?
with open(r'D:\Programmi\Linguaggi\Python\testingyaml.yml','r+') as cocktails:
buffer = yaml.safe_load(cocktails)
buffer['one'] = 'two'
yaml.dump(buffer,cocktails)
如果我這樣做,它會添加新的鍵和值,但它也會將整個 yaml 文件添加到自身
您需要創建另一個可寫緩沖區,可能是這樣的:
with open(r'D:\Programmi\Linguaggi\Python\testingyaml.yml', 'r+') as cocktails:
buffer = yaml.safe_load(cocktails)
buffer['one'] = 'two'
with open(r'D:\Programmi\Linguaggi\Python\testingyaml.yml', 'w') as cocktails:
yaml.dump(buffer, cocktails)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.