如何在构建 Json 字符串时清除数组并重置 for 循环中的值?

How to clear an array and reset the values in a for loop while building a Json string?

我正在使用 openpyxl 导入循环遍历 excel sheet 中的每一行,最终构建一个大的 Json 字符串,我可以将其提供给 API。

我遍历每一行并构建我的 json 结构,我需要用“||”拆分单元格值,然后对于每个值,它需要作为嵌套数组添加到json 节。我目前正在使用以下代码,我的问题是我在我的 for 循环中构建我的列表对象并将 json 块附加到更大的数组,并且它在每个循环期间不断附加我的列表值。所以我在列表中使用 .Clear() 方法在每次循环后清除它......但是当我编译我的最终输出时我的列表是空的。它就像它在每个循环添加到列表时不维护它的值一样。我是 Python 的新手,试用了一下。任何正确方向的建议将不胜感激。它几乎就像每个循环都需要自己独特的数组来使用和保存值。 Json 的标签部分在每个 json 行的最终输出中被清空...当它应该包含每个唯一迭代的值时。

我的数据集(我在 excel 中有 3 行)。您可以看到我在第 7 列中有要拆分的值。这是我正在循环拆分值的列,因为它们将嵌套在我的 json.

第 1 行(单元格)= "ABC","Testing","Testing Again","DATE","DATE",空,"A || B || C".

第 2 行(单元格)= "ABC 2","Testing 2","Testing Again 2","DATE","DATE",空,"X || Y || Z".

第 3 行(单元格)= "ABC 3"、"Testing 3"、"Testing Again 3"、"DATE"、"DATE"、空、空。

我的代码。

#from openpyxl import Workbook
import json
from openpyxl import load_workbook

output_table = input_table.copy()

var_path_excel_file = flow_variables['Location']

workbook = load_workbook(filename=var_path_excel_file)
sheet = workbook.active

#create a null value to be used
emptyString = "Null"

#list out all of the sections of the json that we want to print out - these are based on the inputs
jsonFull = []
jsondata = {}
tags = []

for value in sheet.iter_rows(min_row=2,min_col=0,max_col=40,values_only=True):  

    #I add my split values to an array so that way when i add the array to the json it will have the proper brackets i need for the API to run correctly
    if value[6] is not None:
        data = value[6].split(" || ")
        for temp in data:           
            tags.append(temp)           

    #I build out the json structure here that will be added for each excel row basically
    jsondata = {
        "name": value[0],
        "short_description": value[1],
        "long_description": value[2],
        "effective_start_date": value[3],
        "effective_end_date": value[4],
        "workflow_state": emptyString,              
        "tags": tags
        }

    #Add the jsondata row to the larger collection
    jsonFull.append(jsondata)   
    tags.clear()

print(json.dumps(jsonFull))

然后我想要的结果就是这样。我只需要弄清楚列表处理的正确语法...但似乎找不到作为基础的示例。

[
    {
        "name": "ABC", 
        "short_description": "Testing", 
        "long_description": "Testing Again", 
        "effective_start_date": "2020-03-04T14:45:22Z", 
        "effective_end_date": "2020-03-04T14:45:22Z", 
        "workflow_state": "Null", 
        "tags": [
            "A",
            "B",
            "C"
        ]
    }, 
    {
        "name": "ABC 2", 
        "short_description": "Testing 2", 
        "long_description": "Testing Again 2", 
        "effective_start_date": "2020-03-04T14:45:22Z", 
        "effective_end_date": "2020-03-04T14:45:22Z", 
        "workflow_state": "Null", 
        "tags": [
            "X",
            "Y",
            "Z"
        ]
    }, 
    {
        "name": "ABC 3", 
        "short_description": "Testing 3", 
        "long_description": "Testing Again 3", 
        "effective_start_date": "2020-03-04T14:45:22Z", 
        "effective_end_date": "2020-03-04T14:45:22Z", 
        "workflow_state": "Null", 
        "tags": [           
        ]
    }
]

当您将 tags 放入字典或调用 tags.clear() 时,您并不是在复制 tags,您只是在引用同一个列表。您需要在每次循环迭代开始时创建一个新列表,而不是重复使用同一个列表。

for value in sheet.iter_rows(min_row=2,min_col=0,max_col=40,values_only=True):  

    #I add my split values to an array so that way when i add the array to the json it will have the proper brackets i need for the API to run correctly
    if value[6] is not None:
        tags = value[6].split(" || ")
    else:
        tags = []         

    #I build out the json structure here that will be added for each excel row basically
    jsondata = {
        "name": value[0],
        "short_description": value[1],
        "long_description": value[2],
        "effective_start_date": value[3],
        "effective_end_date": value[4],
        "workflow_state": emptyString,              
        "tags": tags
        }

    #Add the jsondata row to the larger collection
    jsonFull.append(jsondata)