如何将 1 个单元格与另一列中的多个单元格动态匹配到字典中并转换 json?

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

我有一个这样的电子表格。列数据是动态的,可以以“N”种不同的方式变化。

例如,“h”列可以有“n”个值。我需要将它们与“t”列中的正确值进行匹配,并且每个“t”在“c”列中可以有“n”个值。

所以h1可以有3个匹配的“t”,“t1”可以有2个“c”,就像“t2”可以有3个“c”等等。数字不一样。

注意:h1 和 h2 并不总是序列。例如,我可以有 h1,然后有 h40。电子表格中唯一连续的部分是每个“t”具有的“c”的数量。

电子表格示例

h t c 旗帜
h1 h1 的 t1 c1 来自 t1 h1 0
来自 t1 h1 的 c2 0
h1 的 t2 c1 来自 t2 h1 1
c2 来自 t2 h1 1
c3 来自 t2 h1 1
h1 的 t3 c1 来自 t3 h1 2
h2 h2 的 t1 c1 来自 t1 h2 3
c2 来自 t1 h2 3
来自 h2 的 t2 c1 来自 t2 h2 4
c2 来自 t2 h2 4

并且工作表继续...所以我需要将每个“h”与正确的“t”匹配,每个“t”首先将“c”正确匹配到字典中!

我需要一个与此类似的 JSON。

{
    "h1": {
        "t1 from h1": {
            "0": "c1 from t1",
            "1": "c2from t1"
        },
        "t2 from h1": {
            "2": "c1 from t2",
            "3": "c2 from t2",
            "4": "c3 from t2"
        },
        "t3 from h1": {
            "5": "c1 from t3"
        }
    },
    "h2": {
        "t1 from h2": {
            "6": "c1 from t1",
            "7": "c2from t1"
        },
        "t2 from h2": {
            "8": "c1 from t2",
            "9": "c2from t2"
        }
    }
}

我就是这样得到它的。

[
    {
        "t1 from h1": {
            "0": "c1 from t1 h1",
            "1": "c2 from t1 h1"
        }
    },
    {
        "t2 from h1": {
            "2": "c1 from t2 h1",
            "3": "c2 from t2 h1",
            "4": "c3 from t2 h1"
        }
    },
    {
        "t3 from h1": {
            "5": "c1 from t3 h1"
        }
    },
    {
        "t1 from h2 ": {
            "6": "c1 from t1 h2",
            "7": "c2 from t1 h2"
        }
    },
    {
        "t2 from h 2": {
            "8": "c1 from t2 h2",
            "9": "c2 from t2 h2"
        }
    }
]

这是我更改的代码,所以我现在的问题是我可以匹配每个 h 女巫每个 t 吗?

import pandas as pd
import json


sheet = pd.read_excel("sheet.xlsx")
serie_t = sheet['t'].dropna(how='any').reset_index(drop=True)
df = pd.DataFrame(sheet)
data = []

#match t with c
for indext , t in enumerate(serie_t):
    #filter returns dataframe row by flag column value returns all values ​​using flag value
    filter_c = df[df['flag']== indext]
    #converting dataframe of c column to dictionary
    filtered_c = filter_c['c'].to_dict()
    #inserting the value in the list by the t
    listed = {t:filtered_c}
    data.insert(indext,listed)
    

new_json = json.dumps(data)
with open("new_json.json", "w") as file:
    file.write(new_json)

注意

我尝试过的逻辑。我获取列 t 的索引,并通过标志列的 VALUE 进行检查,并尝试获取 c 的值。如果我在代码中设置值 0 或 1,则代码可以正常工作。但在这种情况下,如果列索引为 0,我会转到标志列并检查该列的 VALUE 是否为 0,然后我得到“c”列中单元格的值

python pandas
1个回答
0
投票

看起来,制作最终虚构作品所需的一切都包含在 c 列中。您可以通过 pandas 获取 c 列作为值列表,或者我将直接使用 openpxl 之类的东西。你想要什么都可以。

一旦你有类似的事情:

column_c = [
    "c1 from t1 h1",
    "c2 from t1 h1",
    "c1 from t2 h1",
    "c2 from t2 h1",
    "c3 from t2 h1",
    "c1 from t3 h1",
    "c1 from t1 h2",
    "c2 from t1 h2",
    "c1 from t2 h2",
    "c2 from t2 h2"
]

您可以利用

setdefault()
产生您想要的结果。

final = {}
for index, value in enumerate(column_c):
    c, _, t, h = value.split(" ")
    final \
        .setdefault(h, {}) \
        .setdefault(f"{t} from {h}", {}) \
        .setdefault(index, f"{c} from {t}") \

import json
print(json.dumps(final, indent=4))

这会给你:

{
    "h1": {
        "t1 from h1": {
            "0": "c1 from t1",
            "1": "c2 from t1"
        },
        "t2 from h1": {
            "2": "c1 from t2",
            "3": "c2 from t2",
            "4": "c3 from t2"
        },
        "t3 from h1": {
            "5": "c1 from t3"
        }
    },
    "h2": {
        "t1 from h2": {
            "6": "c1 from t1",
            "7": "c2 from t1"
        },
        "t2 from h2": {
            "8": "c1 from t2",
            "9": "c2 from t2"
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.