将字典的字典导出到 CSV 文件会将内部值转换为不需要的字符串

发布于 2025-01-17 09:29:56 字数 1155 浏览 0 评论 0原文

我有一个词典,该字典是一个值,值是另一个键的词典,其各自的值为列表。

如下一个示例是密钥655值:

655: {
    {
        0: [],
        1: [],
        2: [],
        3: [],
        4: [],
        5: [],
        6: [],
        7: [],
        8: [
            [299, 0.4444444444444444, 0],
            [627, 0.4444444444444444, 0],
            [300, 0.2222222222222222, 0],
            [628, 0.2222222222222222, 0],
            [301, 0.1111111111111111, 0],
            [629, 0.1111111111111111, 0],
            [302, 0.2222222222222222, 0],
            [630, 0.2222222222222222, 0]
        ],
        9: []
    }
}

我的问题是,当我导出字典intop a csv文件并从另一个脚本导入时,我会得到以下结果:

    '655': [
        '{0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [[299, 0.4444444444444444, 0], [627, 0.4444444444444444, 0], [300, 0.2222222222222222, 0], [628, 0.2222222222222222, 0], [301, 0.1111111111111111, 0], [629, 0.1111111111111111, 0], [302, 0.2222222222222222, 0], [630, 0.2222222222222222, 0]], 9: []}'
    ]
}

我已经对关键问题进行了整理,将其转换回int,但我不喜欢t知道如何将值恢复为字典,而不是将其恢复为字符串。

I have a dictionary that holds as values another dictionary of 10 keys with its respective values as list.

As an example following is key 655 values:

655: {
    {
        0: [],
        1: [],
        2: [],
        3: [],
        4: [],
        5: [],
        6: [],
        7: [],
        8: [
            [299, 0.4444444444444444, 0],
            [627, 0.4444444444444444, 0],
            [300, 0.2222222222222222, 0],
            [628, 0.2222222222222222, 0],
            [301, 0.1111111111111111, 0],
            [629, 0.1111111111111111, 0],
            [302, 0.2222222222222222, 0],
            [630, 0.2222222222222222, 0]
        ],
        9: []
    }
}

My problem is that when I export the dictionary intop a csv file and imported from another script I get the following result:

    '655': [
        '{0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [[299, 0.4444444444444444, 0], [627, 0.4444444444444444, 0], [300, 0.2222222222222222, 0], [628, 0.2222222222222222, 0], [301, 0.1111111111111111, 0], [629, 0.1111111111111111, 0], [302, 0.2222222222222222, 0], [630, 0.2222222222222222, 0]], 9: []}'
    ]
}

I have already sort out the key issue converting it back to int but I don't know how to recover the value as dictionary, instead of getting it as a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2025-01-24 09:29:56

根据@enke的建议,以下内容为我的问题提供了解决方案。

首先,我将数据导入为:

data = {}
with open('probabilities.csv', mode='r') as input:
    reader = csv.reader(input)

    data = {rows[0]:rows[1] for rows in reader}

input.close()

然后,我将主字典中的键及其值重新转换回包含十个键及其值的字典(作为列表)。

probabilities = {}

for k, v in data.items():
    probabilities[int(k)] = ast.literal_eval(v)

获得具有所需结果的新字典:

655: {
{
    0: [],
    1: [],
    2: [],
    3: [],
    4: [],
    5: [],
    6: [],
    7: [],
    8: [
        [299, 0.4444444444444444, 0],
        [627, 0.4444444444444444, 0],
        [300, 0.2222222222222222, 0],
        [628, 0.2222222222222222, 0],
        [301, 0.1111111111111111, 0],
        [629, 0.1111111111111111, 0],
        [302, 0.2222222222222222, 0],
        [630, 0.2222222222222222, 0]
    ],
    9: []
}

}

Following @enke's suggestion the following provides a solution to my issue.

First I import the data as:

data = {}
with open('probabilities.csv', mode='r') as input:
    reader = csv.reader(input)

    data = {rows[0]:rows[1] for rows in reader}

input.close()

Then I reconvert the key from the main dictionary and its values back to a dictionary of ten keys and its values as list.

probabilities = {}

for k, v in data.items():
    probabilities[int(k)] = ast.literal_eval(v)

Obtaining a new dictionary with the desired result:

655: {
{
    0: [],
    1: [],
    2: [],
    3: [],
    4: [],
    5: [],
    6: [],
    7: [],
    8: [
        [299, 0.4444444444444444, 0],
        [627, 0.4444444444444444, 0],
        [300, 0.2222222222222222, 0],
        [628, 0.2222222222222222, 0],
        [301, 0.1111111111111111, 0],
        [629, 0.1111111111111111, 0],
        [302, 0.2222222222222222, 0],
        [630, 0.2222222222222222, 0]
    ],
    9: []
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文