使用PyYAML解析yaml文件时,报错:
fs = open(r"E:virtual_workshopAPP_AutoTestcapscaps.yml") caps_data = yaml.load(fs) print(caps_data) #运行结果 D:programPython37python.exe E:/virtual_workshop/APP_AutoTest/common/base_driver.py [{'platformName': 'Android', 'platformVersion': '5.1.1', 'deviceName': 'Android Emulato', 'appPackage': 'com.xxzb.fenwoo', 'appActivity': 'com.xxzb.fenwoo.activity.addition.WelcomeActivity'}, {'server_ip': '127.0.0.1', 'server_port': 4723}] E:/virtual_workshop/APP_AutoTest/common/base_driver.py:8: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. caps_data = yaml.load(fs) Process finished with exit code 0
原因:YAML 5.1版本后弃用了yaml.load(file)这个用法,因为觉得很不安全,5.1版本之后就修改了需要指定Loader,通过默认加载器(FullLoader)禁止执行任意函数,该load函数也变得更加安全
解决办法:
caps_data = yaml.load(fs, Loader=yaml.FullLoader)