python yml 文件處理


安裝 pip install pyyaml

 

import yaml
import io

s = {'host': {'ip00': '10.0.0.1', 'ip01': {'one': '10.0.0.2', 'two': '10.0.0.3'}},
     'sort': {'apache': 2.2, 'php': 5.3, 'mysql': 5.7}}
# yml 文件寫入
f1 = "ss.yml"
with io.open(f1, 'w', encoding="utf-8") as wf:
    yaml.dump(s, wf)
# yml 讀取 
with io.open("ss.yml", 'r', encoding="utf-8") as rf:
    ss = yaml.load(rf)
    print(ss)
# {'host': {'ip00': '10.0.0.1', 
# 'ip01': {'one': '10.0.0.2', 'two': '10.0.0.3'}}, 
# 'sort': {'apache': 2.2, 'mysql': 5.7, 'php': 5.3}}


# python2 中執行dump 時候 文件中顯示 !!python/unicode
# 解決方式:
with io.open(f1, 'w', encoding="utf-8") as wf:
    yaml.safe_dump(s, wf, default_flow_style=False, line_break=True, indent=4, allow_unicode=True)

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM