Javascript

python 读取json格式数据(Dtt+Pytest)

本文主要是介绍python 读取json格式数据(Dtt+Pytest),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import json
import pytest


def get_data_json():
    '''
    @return: 返回列表数据
    '''
    with open("test.json") as f:
        lst_data = []
        my_data = json.load(f)
        lst_json = my_data['data']
        for one in lst_json:
            tmp_list = []
            for i in one.keys():  # 遍历one字典中的键值对
                tmp_list.append(one[i])  # 获取键对应的值添加到列表中
            lst_data.append(tmp_list)
        return lst_data


@pytest.mark.parametrize('nam, age, six', get_data_json())
def test_json(nam, age, six):
    print(nam, age, six)


# test.json 文件内容如下:
# {
#  "data": [
#     {
#        "name": "tom",
#        "age": 18,
#        "six": "boy"
#     },
#     {
#        "name": "kike",
#        "age": 19,
#        "six": "girl"
#     },
#     {
#        "name": "rose",
#        "age": 20,
#        "six": "boy"
#     }
#  ]
# }

if __name__ == '__main__':
    # print(get_data_json1())
    pytest.main(['-vs', 'test_json.py'])
这篇关于python 读取json格式数据(Dtt+Pytest)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!