網站首頁 編程語言 正文
1.yaml文件
# house.yaml-------------------------------------------------------------------------- # 1."數據結構"可以用類似大綱的"縮排"方式呈現 # 2.連續的項目通過減號“-”來表示,也可以用逗號來分割 # 3.key/value對用冒號“:”來分隔 # 4.數組用'[ ]'包括起來,hash用'{ }'來包括 # ················寫法 1····················· house: family: name: Doe parents: John, Jane children: - Paul - Mark - Simon address: number: 34 street: Main Street city: Nowheretown zipcode: 12345 # ················寫法 2····················· #family: {name: Doe,parents:[John,Jane],children:[Paul,Mark,Simone]} #address: {number: 34,street: Main Street,city: Nowheretown,zipcode: 12345}
"""Read_yaml.py--------------------------------------------------------------------""" import yaml,json with open("house.yaml",mode="r",encoding="utf-8") as f1: res = yaml.load(f1,Loader=yaml.FullLoader) print(res,"完整數據") """訪問特定鍵的值""" print("訪問特定鍵的值",res['house']['family']['parents']) print(type(res)) """字典轉換為json""" transition_json = json.dumps(res) print(transition_json) print(type(transition_json))
2.CSV文件
269,839,558 133,632,294 870,273,311 677,823,536 880,520,889
""" CSV文件讀取 """ """ 1.with語句自動關閉文件 2.文件讀取的方法 read() 讀取全部 返回字符串 readline() 讀取一行 返回字符串 readlines() 讀取全部 返回列表(按行) 3.讀取的數據行末,自動加"\n" """ import os class Read_CSV(object): def __init__(self, csv_path): self.csv_path = csv_path def read_line(self, line_number): try: """【CSV文件的路徑】""" csv_file_path = os.path.dirname(os.path.dirname(__file__)) + self.csv_path with open(csv_file_path, "r") as f1: """ |讀取某一行內容|--->|去掉行末"\n"|--->|以","分割字符串| """ line1 = f1.readlines()[line_number - 1] line1 = line1.strip("\n") list1 = line1.split(",") return list1 except Exception as e: print(f"!!! 讀取失敗,因為 {e}") if __name__ == '__main__': """example = Read_CSV(r"\軟件包名\文件名") """ csv = Read_CSV(r"\CSV_File\data.csv") for i in range(3): print(csv.read_line(1)[i]) csv1 = Read_CSV(r"\CSV_File\random_list.csv") for i in range(3): print(csv1.read_line(3)[i])
3.ini文件
# config.ini-------------------------------------------------------------------- [config_parameter] url=http://train.atstudy.com browser=FireFox [element] a=text class=CSS_Selector
import configparser;import os """ 1.調用【configparser】模塊""" config = configparser.ConfigParser();print(f"config類型 {type(config)}") """ 2.ini文件的路徑""" path1 = os.path.dirname(os.path.dirname(__file__))+r"\Ini_File\config.ini" print(f"ini文件的路徑 {path1}") """ 3.讀取ini文件""" config.read(path1);print(f"config.read(path1) {config.read(path1)}") """【第一種】獲取值""" value = config['config_parameter']['url'] print('config[節點][key]:\t',value) """【第二種】獲取值""" value = config.get('config_parameter','browser') print('config.get(節點,key):\t',value) """【第三種】獲取所有值""" value = config.items('config_parameter') print('config.items(節點):\t',value)
""" 讀取ini文件 """ import configparser import os class ReadIni(object): def __init__(self, file_path, node): self.node = node self.file_path = file_path def get_value(self, key): try: """ 1.調用【configparser】模塊--->2.ini文件的路徑 3.讀取ini文件--->4.根據鍵獲取值 """ config = configparser.ConfigParser() path1 = os.path.dirname(os.path.dirname(__file__)) + self.file_path config.read(path1) value = config.get(self.node, key) return value except Exception as e: print(f"!!! 讀取失敗,因為 {e}") if __name__ == '__main__': """example = ReadIni(r"\軟件包名\文件名","節點名") """ node1 = ReadIni(r"\Ini_File\config.ini", "element") print(node1.get_value("class"))
總結
原文鏈接:https://blog.csdn.net/qq_45513965/article/details/122871290
相關推薦
- 2022-12-12 python?打印dict的key與value方式_python
- 2022-04-23 一起來了解python的基本輸入和輸出_python
- 2021-12-14 常用時間處理方法
- 2023-06-05 Python?time時間格式化和設置時區實現代碼詳解_python
- 2022-07-04 Python如何一行輸入多個數,并存入列表_python
- 2022-08-31 python中ndarray數組的索引和切片的使用_python
- 2023-07-09 windows找不到gpedit.msc,請確定文件名是否正確
- 2022-10-23 python運行或調用另一個py文件或參數方式_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同步修改后的遠程分支