pip install PyYaml
创建一个后缀名为yml的文件
在里面输入
name: "test_yaml"
result: "success"
通过Python代码读取yml文件
import yaml
with open("yaml_demo.yml", "r") as f:
data = yaml.safe_load(f)
print(data)
# 输出结果
{'name': 'test_yaml', 'resule': 'success'}
在yml文件里面写入
- "a"
- "b"
- "c"
然后用继续用Python读取yml文件
import yaml
with open("yaml_demo.yml", "r") as f:
data = yaml.safe_load(f)
print(data)
# 输出结果
['a', 'b', 'c']
在Yml文件中编辑
person1:
name: "connor"
age: "18"
person2:
name: "amy"
age: "20"
用Python读取文件
import yaml
with open("yaml_demo.yml", "r") as f:
data = yaml.safe_load(f)
print(data)
# 输出结果
{'person1': {'name': 'connor', 'age': '18'}, 'person2': {'name': 'amy', 'age': '20'}}
在Yml文件中编辑
person1:
- "connor"
- "18"
person2:
- "Amy"
- "20"
用Python读取文件
import yaml
with open("yaml_demo.yml", "r") as f:
data = yaml.safe_load(f)
print(data)
# 输出结果
{'person1': ['connor', '18'], 'person2': ['Amy', '20']}
-
- "a"
- "b"
- "c"
-
- "e"
- "f"
- "d"
用Python读取文件
import yaml
with open("yaml_demo.yml", "r") as f:
data = yaml.safe_load(f)
print(data)
# 输出结果
[['a', 'b', 'c'], ['e', 'f', 'd']]
- username: "connor"
- age:
A: "20"
B: "30"
用Python读取文件
import yaml
with open("yaml_demo.yml", "r") as f:
data = yaml.safe_load(f)
print(data)
# 输出结果
[{'username': 'connor'}, {'age': {'A': '20', 'B': '30'}}]
上面展示的都是读取一个文档,yaml可以通过添加---
来分割多个文档,并且Python的读取方法要用yaml.safe_load_all()
---
用户名: "connor"
密码: "123456"
---
用户名: "AMY"
密码: "456789"
用Python读取文件
import yaml
with open("yaml_demo.yml", "r", encoding="utf8") as f: # 由于yml文件有中文, 因此要加上encoding="utf8"
data = yaml.safe_load(f) # 返回的是generator对象
for i in data:
print(i) # 循环对象并打印结果
# 输出结果
{'用户名': 'connor', '密码': '123456'}
{'用户名': 'AMY', '密码': '456789'}
创建utils目录并创建Yamlutil.py文件,在里面定义一个工具类YamlReader
,我需要它在初始化的时候判断yml文件是否存在,然后定义读取单个文档的方法read_data
和多个文档的方法read_all
。
实现代码:
import os
import yaml
# 1. 创建类
class YamlReader:
# 2. 初始化方法, 判断文件是否存在
def __init__(self, yamlfile):
if os.path.exists(yamlfile):
self.yamlf = yamlfile
else:
raise FileNotFoundError("文件不存在")
self._data = None # 默认没有读取过文件
self._data_all = None # 默认没有读取过文件
# 定义方法, Yaml读取单个文档、多个文档
def read_data(self):
if not self._data: # 判断是否有读取文件
with open(self.yamlf, "r", encoding="utf8") as f:
self._data = yaml.safe_load(f)
return self._data
def read_all(self):
if not self._data_all: # 判断是否有读取文件
with open(self.yamlf, "r", encoding="utf8") as f:
self._data_all = list(yaml.safe_load_all(f))
return self._data_all
然后通过调用这个工具类来试试读取yml文件
from utils.Yamlutil import YamlReader
# 初始化yamutils, 传入文件名称
readr = YamlReader(yamlfile="yaml_demo.yml")
读取单个文档的yml
# 调用方法,输出结果
data = readr.read_data() # 读取单个文件
print(data)
#返回结果
[['a', 'b', 'c'], ['e', 'f', 'd']]
读取单个文档的yml
# 调用方法,输出结果
data = readr.read_all() # 读取多个文件
print(data)
#返回结果
[[['a', 'b', 'c'], ['e', 'f', 'd']], {'用户名': 'connor', '密码': '123456'}, {'用户名': 'AMY', '密码': '456789'}]
由于上面已经将读取yml文件的工具类YamlReader写好了, 因此只需要创建一个yml并写入appium的连接配置即可
platformName: "Android"
platformVersion: "10"
deviceName: "CLT-AL00"
appPackage: "com.gotokeep.keep"
appActivity: "com.gotokeep.keep.refactor.business.main.activity.MainActivity"
然后通过调用工具类YamlReader读取yml文件即可
from appium import webdriver
from utils.Yamlutil import YamlReader
import time
reader = YamlReader("../conf/appium_config.yaml") # 读取yml文件
desired = reader.read_data()
# print(desired)
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired)
# time.sleep(5)
driver.close()