今天用之前的代碼寫新項目,運行代碼報錯:YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe.git
去瞅了一眼,發現yaml.load(input)棄用了,隨後發現,5.1以後的須要加一個加載器才能夠正常運行,針對該變化,做了如下比較github
yaml 5.1以前的使用方法:安全
1 def operateYaml(self,filename): 2 file = open(filename, "r",encoding='utf-8') 3 data = yaml.load(file) 4 file.close() 5 return data
yaml 5.1以後的使用方式有兩種:spa
1 def operateYaml(self,filename): 2 file = open(filename, "r",encoding='utf-8') 3 data = yaml.load(file,Loader=yaml.FullLoader) #loader可選擇BaseLoader、SafeLoader、FullLoader、UnsafeLoader 4 file.close() 5 return data
1 def operateYaml(self,filename): 2 file = open(filename, "r",encoding='utf-8') 3 data = yaml.full_load(file) #可以使用3種不一樣的加載方式:yaml.full_load、yaml.unsafe_load
4 file.close()
5 return datayaml.safe_load、
針對不一樣的須要,加載器有以下幾種類型:code
yaml.load(input)
(發出警告後)(full_load)Loader
向後兼容性):原始的Loader代碼,能夠經過不受信任的數據輸入輕鬆利用。(unsafe_load) 詳情請查看——https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation#footnotesblog