網站首頁 編程語言 正文
1.關于yaml
yaml基本語法規則:
- 大小寫敏感
- 使用縮進表示層級關系
- 縮進時不允許使用
Tab
鍵,只允許使用空格。 - 縮進的空格數目不重要,只要相同層級的元素左側對齊即可
- #表示注釋,從這個字符一直到行尾,都會被解析器忽略,這個和
python
的注釋一樣
2.yaml數據結構
YAML 支持的數據結構有三種:
- 對象
鍵值對的集合,又稱為映射(mapping)/ 哈希(hashes) / 字典(dictionary)
對象的一組鍵值對,使用冒號結構表示。
- 數組
一組按次序排列的值,又稱為序列(sequence) / 列表(list)
一組連詞線開頭的行,構成一個數組。
- 純量(
scalars
)
單個的、不可再分的值
包括字符串,布爾值,整數,浮點數,Null,時間,日期
3.yaml文件格式
auth.login: ? data: ? ? name: '18888888883' ? ? password: jnyj123456 ? url: https://XXXX-api-XXXX.zje.com/auth/login headers: ? Accept: '*/*' ? Accept-Encoding: gzip, deflate, br ? Accept-Language: zh-CN,zh;q=0.9 ? Connection: keep-alive ? Content-Length: '46' ? Content-type: application/json ? Host: dexin-api-test.zje.com ? Origin: https://XXXX-spa-XXX.zje.com ? Referer: https://XXXX-spa-XXX.zje.com/ ? Sec-Fetch-Dest: empty ? Sec-Fetch-Mode: cors ? Sec-Fetch-Site: same-site ? User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, ? ? like Gecko) Chrome/98.0.4758.80 Safari/537.36 ? authorization: Bearer ? sec-ch-ua: '" Not A;Brand";v="33" ? sec-ch-ua-mobile: ?0000 ? sec-ch-ua-platform: macOSis
學習產出:
class OpenYaml(object): ? ? def __init__(self): ? ? ? ? self.file_path = os.path.join(route("/DataYaml/yaml.yaml")) ?# 拼接讀取的文件路徑 ? ? def open(self, *args): ? ? ? ? ''' ? ? ? ? ? ?args[0]: 字典名稱 ? ? ? ? ? ?args[1]: 字段值 ? ? ? ? ? ?讀取文件 ? ? ? ? ''' ? ? ? ? try: ? ? ? ? ? ? if len(args) == 2: ?# 根據傳值判斷執行內容 ? ? ? ? ? ? ? ? with open(self.file_path, "r") as f: ?# 讀取yaml ? ? ? ? ? ? ? ? ? ? Json = f.read() ?# 獲取yaml ? ? ? ? ? ? ? ? ? ? Dict = yaml.safe_load(Json)[args[0]] ?# 提取制定內容 ? ? ? ? ? ? ? ? if args[1] in Dict.keys(): ?# 判斷key是否存在 ? ? ? ? ? ? ? ? ? ? logs.info(f"yaml文件,查找內容成功,內容:{Dict[args[1]]}") ? ? ? ? ? ? ? ? ? ? return Dict[args[1]] ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? print(f"對應字段{args[1]}不存在...") ? ? ? ? ? ? ? ? ? ? logs.info(f"對應字段{args[1]}不存在...") ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? with open(self.file_path, "r") as f: ? ? ? ? ? ? ? ? ? ? Json = f.read() ? ? ? ? ? ? ? ? ? ? Dict = yaml.safe_load(Json)[args[0]] ? ? ? ? ? ? ? ? return Dict ? ? ? ? except Exception as e: ? ? ? ? ? ? print(f'讀取yaml文件,報錯:{e}') ? ? ? ? ? ? logs.info(f'讀取yaml文件,報錯:{e}') ? ? def Wri_file(self, *args): ? ? ? ? ''' ? ? ? ? :param args: args[0] 接口字段、args[1] key、 args[2] value ? ? ? ? :return: None ? ? ? ? 把字段寫入yaml ? ? ? ? ''' ? ? ? ? try: ? ? ? ? ? ? with open(self.file_path, encoding="utf-8") as f: ?# 讀取文件 ? ? ? ? ? ? ? ? data = yaml.load(f.read(), Loader=yaml.FullLoader) ?# 獲取讀取內容 ? ? ? ? ? ? print(data[args[0]]) ? ? ? ? ? ? if data is not None: ?# 判斷讀取內容是否為空 ? ? ? ? ? ? ? ? if str(data[args[0]][args[1]]) in str(data[args[0]]): ?# 判斷name是否存在在dict ? ? ? ? ? ? ? ? ? ? data[args[0]][args[1]] = args[2] ? ? ? ? ? ? ? ? ? ? with open(self.file_path, 'w', encoding="utf-8") as f: ?# 寫入 ? ? ? ? ? ? ? ? ? ? ? ? yaml.dump(data, stream=f, allow_unicode=True) ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? print("寫入文件的字段不存在!寫入失敗...") ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? logs.info("寫入文件的返回值為空!不能寫入...") ? ? ? ? except Exception as y: ? ? ? ? ? ? logs.info(f"寫入文件失敗:{y}") if __name__ == "__main__": ? ? OpenYaml().Wri_file("headers", "Content-Length", "22") ?? ?OpenYaml().open("auth.login", "data")
原文鏈接:https://blog.csdn.net/weixin_44750991/article/details/123584148
相關推薦
- 2023-12-26 錯誤代碼: 1231Variable ‘sql_notes‘ can‘t be set to the
- 2021-12-11 Android?NDK開發(C語言字符串)_Android
- 2022-04-28 C#中Razor模板引擎簡單使用_C#教程
- 2022-03-27 基于PyQt5制作Excel數據分組匯總器_python
- 2023-01-02 Kotlin?FrameLayout與ViewPager2控件實現滾動廣告欄方法_Android
- 2022-09-27 Android內置的OkHttp用法介紹_Android
- 2022-12-08 用C語言求解第N項斐波那契數列問題_C 語言
- 2022-09-16 利用Python統計Jira數據并可視化_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支