欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用yaml格式文件的方法

 更新時間:2023年07月09日 09:56:17   作者:qq_44659804  
本文主要介紹了python使用yaml格式文件的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

安裝PyYAML安裝包

YAML在python語言中有PyYAML安裝包,下載地址:https://pypi.python.org/pypi/PyYAML

聯(lián)網(wǎng)的windows可直接win+r cmd進(jìn)入終端pip install PyYAML

配置yaml文件

進(jìn)入相應(yīng)python文件的目錄,創(chuàng)建yaml文件(略)

通過編程軟件創(chuàng)建

自定義文件后綴為 .yaml

編寫yaml文件內(nèi)容

city: shanghai
time: 202210
successfully: 200
exception: 500

python讀取yaml文件

#使用utf-8編碼
# -*- coding:utf-8 -*-
#導(dǎo)入yaml模塊
import yaml
f = open('test.yaml', 'r', encoding='utf-8')  #讀yaml文件,編碼用utf-8
cfg = f.read()  #讀全部文件
d = yaml.load(cfg, Loader=yaml.FullLoader) # 用load方法把讀到的yaml文件內(nèi)容轉(zhuǎn)換成字典類型
# yaml5.1版本后棄用了yaml.load(file)這個用法,因為覺得很不安全,5.1版本之后就修改了需要指定Loader,通過默認(rèn)加載器(FullLoader)禁止執(zhí)行任意函數(shù)
# Loader=yaml.FullLoader 加上這行代碼,告警就沒了
print(d)

結(jié)果為字典類型

{'city': 'shanghai', 'time': 202210, 'successfully': 200, 'exception': 500}

yaml文件與python代碼書寫比較

yaml基本語法規(guī)則:
鍵值對
大小寫敏感
使用縮進(jìn)表示層級關(guān)系
縮進(jìn)時不允許使用Tab鍵,只允許使用空格。
縮進(jìn)的空格數(shù)目不重要,只要相同層級的元素左側(cè)對齊即可
#表示注釋,從這個字符一直到行尾,都會被解析器忽略,這個和python的注釋一樣
yaml支持的數(shù)據(jù)結(jié)構(gòu)有三種:
對象:鍵值對的集合,又稱為映射(mapping)/ 哈希(hashes) / 字典(dictionary)
數(shù)組:一組按次序排列的值,又稱為序列(sequence) / 列表(list)
純量(scalars):單個的、不可再分的值。字符串、布爾值、整數(shù)、浮點數(shù)、Null、時間、日期

字典嵌套字典

#字典嵌套字典
#python代碼書寫為{'dd': 'city2:shanghai2 time2:2022102'}
#dd:
#  city2: shanghai2
#  time2: 2022102

列表(或叫數(shù)組)中嵌套字典,字典中一組數(shù)據(jù)

#列表(或叫數(shù)組)中嵌套字典
#python代碼書寫為[{'city2': 'shanghai2'}, {'time2': 2022102}, {'successfully': 200}]
#yaml里面寫一個列表,前面加一個'-'符號
- city2: shanghai2
- time2: 2022102
- successfully: 200

列表(或叫數(shù)組)中嵌套字典,字典中多組數(shù)據(jù)

#列表(或叫數(shù)組)中嵌套字典
#python代碼書寫為[{'city3': 'shanghai3', 'time3': 2022103}, {'city4': 'shanghai4', 'time4': 2022104}, {'city5': 'shanghai5', 'time5': 2022102}]
- city3: shanghai3
  time3: 2022103
- city4: shanghai4
  time4: 2022104
- city5: shanghai5
  time5: 2022102

字典嵌套列表

#字典嵌套列表
#python代碼書寫為{'color1': ['red', 1], 'color2': ['bule', 2], 'color3': ['black', 3]}
color1:
  - red
  - 1
color2:
  - bule
  - 2
color3:
  - black
  - 3

組合使用

#組合使用
#python代碼書寫為{'languages': ['Ruby', 'Perl', 'Python', 'java c++ shell'], 'websites': {'YAML': 'yaml.org', 'Ruby': 'ruby-lang.org', 'Python': 'python.org', 'Perl': 'use.perl.org'}, 'db': {'host': 'xxx', 'port': 3306, 'user': 'shanghai', 'password': 'xxx', 'db_name': 'china', 'db_type': 'mysql'}}
languages:
 - Ruby
 - Perl
 - Python
 - java c++ shell
websites:
 YAML: yaml.org
 Ruby: ruby-lang.org
 Python: python.org
 Perl: use.perl.org
db:
    host: xxx
    port: 3306
    user: shanghai
    password: xxx
    db_name: china
    db_type: mysql

其他類型

#1、數(shù)值直接以字面量的形式表示
#number: 202210.010101 #{'number': 202210.010101}
#2、布爾值用true和false表示
#isSet: true #{'isSet': True}
#isSet1: false #{'isSet1': False}
#3、null用~表示
#parent: ~   #{'parent': None}
#4、時間采用 ISO8601 格式。
#time1: 2022-10-10t10:10:10.10-10:00  #{'time1': datetime.datetime(2022, 10, 10, 10, 10, 10, 100000, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=50400)))}
##5、日期采用復(fù)合 iso8601 格式的年、月、日表示。
#date: 2022-10-01  #{'date': datetime.date(2022, 10, 1)}
#6、YAML 允許使用兩個感嘆號,強(qiáng)制轉(zhuǎn)換數(shù)據(jù)類型,轉(zhuǎn)換成字符串str。
#int_to_str: !!str 123  #{'bool_to_str': '123'}
#bool_to_str: !!str true #{'bool_to_str': 'true'}

到此這篇關(guān)于python使用yaml格式文件的方法的文章就介紹到這了,更多相關(guān)python使用yaml內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論