如何将XLS文件与json文件进行比较?

问题描述 投票:0回答:1

嗨,我有一个任务将一些XLS文件与具有工作代码的json文件进行比较,但是循环仅比较xls的第一行,而不转到下一行。

def verify_xlsx_file_content_against_json(json, xlsx_path):

    found_configs_ct = 0
    xlsx_reader = XLSXReader()
    data_frame = xlsx_reader.read_xlsx(xlsx_path, "Configuration Set")
    for config in json_definition:
        items_compared = 0
        for item in config["content"]["items"]:
            for index, row in data_frame.iterrows():
                if config["segAlias"] == row["Seg Alias"] and item["itemsOrder"] == row["Items Order"]:
                    found_configs_ct += 1
                    if index == 0:
                        def_platforms = config["platforms"]
                        platforms = ["gp", "ios", "win32", "fb", "amazon"]
                        for platform in platforms:
                            if platform in def_platforms:
                                assert_equal_values(row[convert_def_platform_to_excel_platform_name(platform)], "Y","{" + platform + "} should be True! and is not.")
                            else:
                                assert_equal_values(row[convert_def_platform_to_excel_platform_name(platform)], "N","{" + platform + "} should be False! and is not.")
                        name_from_definition = config["content"]["items"][0]["name"]  # all items have the same name thus index 0 is sufficient
                        assert_equal_values(row["Name"], name_from_definition, "Content name is invalid")
                    Logger.LogInfo(str(item["identifier"]) + ' identifier')
                    Logger.LogInfo(str(row["Identifier"]) + ' Identifier')
                    Logger.LogInfo(str(item["itemsOrder"]) + ' itemsOrder')
                    Logger.LogInfo(str(row["Items Order"]) + ' Items Order')
                    Logger.LogInfo(str(item["dValue"]) + ' dValue')
                    Logger.LogInfo(str(row["D Value, $"]) + ' D Value, $')
                    Logger.LogInfo(str(row["Pack Image"]) + ' PackImage from row  <<<<<<<<<<<<<<<')
                    Logger.LogInfo(str(item["packImage"]) + ' packImage second from item-<<<<<<<<')
                    assert_equal_values(str(item["identifier"]), str(row["Identifier"]), "Identifier is not match!")
                    assert_equal_values(str(item["itemsOrder"]), str(row["Items Order"]), "Items Order is not match!")
                    assert_equal_values(str(item["packImage"]), str(row["Pack Image"]), "packImage is not match!")
                    assert_equal_values(str(item["dValue"]), str(row["D Value, $"]),"dValue  not match!")
                    # assert_equal_values(str(item["remarks"]), str(row["Remarks"]), "Remarks is not match!")
                    items_compared += 1
                    break
        assert_equal_values(items_compared, len(
            config["content"]["items"]), "number of items compared is less than actual items")
    return False
    logging.info("verification done. ---no problems detected--")
    return True  # if code got here, no errors on the way, return True

我在这里得到的是

NFO:root:[2020-02-14 09:33:24.543957] 0 identifier
      INFO:root:[2020-02-14 09:33:24.543957] -1 Identifier
      INFO:root:[2020-02-14 09:33:24.543957] 1 itemsOrder
      INFO:root:[2020-02-14 09:33:24.543957] 1 Items Order
      INFO:root:[2020-02-14 09:33:24.543957] 1.99 dValue
      INFO:root:[2020-02-14 09:33:24.543957] 1.99 D Value, $
      INFO:root:[2020-02-14 09:33:24.544953] 1 identifier
      INFO:root:[2020-02-14 09:33:24.544953] -1 Identifier
      INFO:root:[2020-02-14 09:33:24.544953] 2 itemsOrder
      INFO:root:[2020-02-14 09:33:24.544953] 1 Items Order
      INFO:root:[2020-02-14 09:33:24.544953] 2.99 dValue
      INFO:root:[2020-02-14 09:33:24.544953] 1.99 D Value, 
      INFO:root:[2020-02-14 09:33:24.544953] 2 identifier
      INFO:root:[2020-02-14 09:33:24.545949] -1 Identifier

所以-1标识符始终为-1,并且应像json文件中那样递增(0,1,2 ...)进行更改。如何更改此循环以使此代码起作用?然后声明这些文件json vs xls

python json xlsx
1个回答
0
投票

您将需要阅读excel文件,并使用熊猫将其转换为json。从熊猫转换时,还要检查您的json文件采用哪种格式。也许您需要更改方向。

df = pd.read_excel(xlsx, sheet_name=sheet)

json_from_excel = df.to_json(orient='records')

接下来,您需要在这里订购json数据,这是一个示例函数。

def ordered(obj):
    if isinstance(obj, dict):
        return sorted((k, ordered(v)) for k, v in obj.items())
    if isinstance(obj, list):
        return sorted(ordered(x) for x in obj)
    else:
        return obj

最后您可以进行比较。

if ordered(json_from_excel) == ordered(json_data):
© www.soinside.com 2019 - 2024. All rights reserved.