安裝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)