python-将所有的json文件写入到yaml

实现功能,将文件夹下所有的json文件读取,并按规定的格式写入到另外一个文件中,生成yaml文件。

后期可能还需要完善。

import json
import os
from ruamel import yaml


def change_yaml():
    #读取文件夹下的文件
    dir1=os.listdir()
    # print(os.listdir())
    dir2=list()
    dir3=list()
    for i in range(len(dir1)):
     if "py" not in dir1[i]:
         dir2.append(dir1[i])
         # print(os.listdir(dir[i]))

    # print(dir2)
#获取yaml文件名
    test=list()
    test1=list()
    test2=list()
    #定义json文件路径dirjson
    dirjson=list()

    for i in range(len(dir2)):
        # print(os.listdir(dir2[i]))
        for j in range(len(os.listdir(dir2[i]))):
            # print(os.path.splitext(os.listdir(dir2[i])[j]))
            # print(type(os.path.splitext(os.listdir(dir2[i])[j])))
            # 获取json文件路径
            pwddata = os.getcwd() + '\\' + dir2[i] + '\\' + os.listdir(dir2[i])[j]
            # print(os.getcwd() + '\\' + dir2[i] + '\\' + os.listdir(dir2[i])[j])
            dirjson.append(pwddata)
            #读取json文件
            with open(pwddata, 'r',encoding='utf-8') as f:
                x = json.dumps(json.load(f))
                #追加json文件到test1列表
                test1.append(x)
            #获取json文件名
            test2.append(dir2[i]+'_'+os.path.splitext(os.listdir(dir2[i])[j])[0])
            test.append(dir2[i]+str(j)+'_test')
    # print(dirjson)
    # for m in range(len(test1)):
    #     print(test1[m])
    #     print(json.dumps(json.loads(test1[m])))

    for m in range(len(test)):
        if 'assert' in test[m]:
            posturi = '/assert'
        elif 'channel' in test[m]:
            posturi = '/config'
        elif 'industry' in test[m]:
            posturi = '/inderstry'
        elif 'parser' in test[m]:
            posturi = '/parser'
        elif 'sink' in test[m]:
            posturi = '/writer'
        elif 'source' in test[m]:
            posturi = '/datasource'
        else:
            posturi = '/error'
        pwdname = test[m] + '.yaml'
        pwd = '..//Config//Params//xyversion//' + pwdname
        with open(pwd, 'w', encoding='utf-8') as f:
            # 准备数据
            class Dag:
                def __init__(self):
                    self.case_suite = test[m]
                    self.descrpiton = test[m]
                    with open(dirjson[m], 'r', encoding='utf-8') as f_load:
                        dic2 = json.load(f_load)
                    dic1 = [{
                        'moduel_class': 'TestDatasource',
                        'desc': test[m],
                        'cases': [{
                            'story': 'test_'+test[m],
                            'desc': test[m],
                            'uri': posturi,
                            'method': 'post',
                            'type': 'json',
                            'headers':
                                {
                                    'Content-Type': 'application/json',
                                    'Token': '@super_token'},
                            "data": dic2,
                            "set_up": "",
                            "tear_down": "",
                            "asserts": "200"
                        }],
                    }
                    ]
                    self.moduels = dic1
        with open(pwd,'w',encoding='utf-8') as stream:
            data = Dag()
            yaml.dump(data.__dict__, stream=stream, Dumper=yaml.RoundTripDumper, allow_unicode=True)




if __name__ == '__main__':
    data=change_yaml()

 

你可能感兴趣的:(开发)