python 读写yaml

安装yaml:

pip install pyyaml


写入yaml:
import json from ruamel import yaml aproject ={'cardBalance': 18850} aproject2 ={"cardNumber": "22222222"} # # print(aproject) # # def th(cas): # # f = open(r'te.yml','r+') # # test=(yaml.dump(cas,f)) # 转换json str # # f.close() # def th(ca): # with open('te.yml', 'r+') as f: # f.write(yaml.dump(ca,Dumper=yaml.RoundTripDumper)) with open('te.yml', 'r+',encoding="utf-8") as f: # 生产字典 如果不使用 Dumper=yaml.RoundTripDumper 写入的数据yml是带{a:124} f.write(yaml.dump(aproject,Dumper=yaml.RoundTripDumper)) f.write(yaml.dump(aproject2, Dumper=yaml.RoundTripDumper)) 读取:

 

import yaml, requests, json
import requests
d = open('b1.yml')
e = yaml.load(d) #转换成字典格式

request = e['request1'] #取这个字典值
print(request)
# # # name = e['name']
# # # method = request['method']
url = request['url']
# print(url)
# # # # # headers = request['headers']
# data = request['data']
# print(data)
test=json.dumps(request['data'])
print(test)
re = requests.post(url,data=test)
print(re.text)

 



  

posted @ 2018-10-08 17:04  mahaining  阅读(2778)  评论(0编辑  收藏  举报