import yaml,os
from ruamel import yaml
desired_caps = {
'platformName': 'Android',
'platformVersion': '7.0',
'deviceName': 'A5RNW18316011440',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'automationName': 'Uiautomator2',
'unicodeKeyboard': [True,"hh"],
'resetKeyboard': True,
'noReset': True,
'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
}
#获取当前脚本所在的文件夹路径
curpath=os.path.dirname(os.path.realpath(__file__))
#获取yaml文件路径
yamlpath=os.path.join(curpath,"caps.yaml")
#写入到yaml文件
with open(yamlpath,"w",encoding="utf-8") as f:
yaml.dump(desired_caps,f,Dumper=yaml.RoundTripDumper)
#open方法打开读取yaml文件
a=open(yamlpath,"r",encoding="utf-8")
print(yaml.load(a.read(),Loader=yaml.Loader))
cfg=a.read()
print(type(cfg)) #读出来的是字符串
print(cfg)
d=yaml.load(cfg)#用load方法转字典
print(d)
print(type(d))