首页 > 解决方案 > 如果值是列表格式的列表,如何从字典中访问各个值?

问题描述

我在 json 中为我的测试功能编写了一个配置文件。配置文件如下所示:

{

    "Chrome_executable_path": "C:\\Users\\AIGHOSH\\PycharmProjects\\chromedriver.exe",
    "File_locations": [["C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupAge8277.csv"],
        ["C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupAge8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupArea8277.csv"],
        ["C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupAge8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupArea8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupEthnic8277.csv"],
        ["C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupAge8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupArea8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupEthnic8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupSex8277.csv"],
        ["C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupAge8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupArea8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupEthnic8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupSex8277.csv", "C:\\Users\\AIGHOSH\\Desktop\\DQ_DATA\\DimenLookupYear8277.csv"]
    ],
    "Null_values": [["_"], ["@"], ["<>"], ["-"], ["#"]],
    "Operation": [["None"], ["Append"], ["Merge"], ["Append then Merge"]],
    "weights": [
        {
        "completeness": "0.2",
        "timeliness": "0.2",
        "uniqueness": "0.2",
        "validity": "0.2",
        "integrity": "0.2"
    },
        {
    "completeness": "0.1",
    "timeliness": "0.3",
    "uniqueness": "0.2",
    "validity": "0.2",
    "integrity": "0.2"
    },
        {
    "completeness": "0.2",
    "timeliness": "0.2",
    "uniqueness": "0.3",
    "validity": "0.1",
    "integrity": "0.2"
    },
        {
    "completeness": "0.2",
    "timeliness": "0.2",
    "uniqueness": "0.1",
    "validity": "0.4",
    "integrity": "0.1"
    },
        {
    "completeness": "0.1",
    "timeliness": "0.1",
    "uniqueness": "0.4",
    "validity": "0.2",
    "integrity": "0.2"
    }
    ]
}

现在我希望 value[0] 成为我的第一个测试用例参数,然后 v[1] 成为我的第二个测试用例参数。

我的测试用例功能:

   #for the fist element of the values(eg: v[0])
   def test_case(self): 
     self.dqopenPage = DqOpenPage(self.driver)    
     self.dqopenPage.drop(filess = config["File_locations"]) #should take the first list with only 1 file
     self.dqopenPage.df_operation(operation=config["Operations"]) #should take the "None"
     self.dqopenPage.define_null_value(null = config["Null_values"])#should take "_"

那么如何使用 for 循环获取值并为所有对应的值(如 v[1]、v[2] 等)运行测试用例函数。

标签: pythonjsondictionarynested-lists

解决方案


您可以像这样访问此处的各个值,

print(config["File_locations"][0][0])
print(config["Null_values"][0][0])
print(config["Operation"][0][0])

如果您想使用循环访问单个值,您可以这样做

这是遍历文件位置的方法。

for file in config["File_locations"]:
    for index in range(len(file)):
        print(file[index])

这是循环空值或操作的方法,

for value in config["Null_values"]:
    print(value[0])

我真的不明白你想要循环遍历哪些列表,但我希望这对你有所帮助。


推荐阅读