【问题标题】:Convert this python dictionary into JSON format? [duplicate]将此 python 字典转换为 JSON 格式? [复制]
【发布时间】:2013-06-14 13:47:03
【问题描述】:

我有一个 python 字典,我想将它转换成 json。

Python 字典:

{"20090209.02s1.1_sequence.txt": [645045714, 3559.6422951221466, 206045184], "20090209.02s1.2_sequence.txt": [645045714, 3543.8322949409485, 234618880]}

期望的输出:

{
       "file_name":"20090209.02s1.1_sequence.txt",
       "file_information": [645045714, 3559.6422951221466, 206045184],

    {
       "file_name":"20090209.02s1.2_sequence.txt",
       "file_information": [645045714, 3543.8322949409485, 234618880],

    }
}

尝试使用json.dumps,但没有得到想要的输出。

【问题讨论】:

  • @AndyHayden:这里还有更多事情要做。这不是一个漂亮的打印问题。
  • 你的输出是 invalid JSON;你的意思是让这成为一个字典列表吗?
  • @MartijnPieters 知道它是什么?!
  • @AndyHayden:我在下面做了一个有根据的猜测。让我们看看这是否足够好。
  • 是的,我提到的欲望输出无效。 @Icfseth 给出的答案正是我想要的。

标签: python json


【解决方案1】:

你需要先创建一个格式正确的结构:

import json

dict_ = {"20090209.02s1.1_sequence.txt": [645045714, 3559.6422951221466, 206045184], "20090209.02s1.2_sequence.txt": [645045714, 3543.8322949409485, 234618880]}
values = [{"file_name": k, "file_information": v} for k, v in dict_.items()]
json.dumps(values, indent=4)

请注意,所需的 JSON 输出对我来说似乎无效。这是这段代码的输出:

[
    {
        "file_name": "20090209.02s1.1_sequence.txt", 
        "file_information": [
            645045714, 
            3559.6422951221466, 
            206045184
        ]
    }, 
    {
        "file_name": "20090209.02s1.2_sequence.txt", 
        "file_information": [
            645045714, 
            3543.8322949409485, 
            234618880
        ]
    }
]

【讨论】:

  • 是的,这就是我想要的。我会尽可能接受你的回答。
【解决方案2】:

将您的键值对拆分为单独的字典:

json.dumps([{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()])

请注意,您的输出的 order 将是任意的(字典没有固定的顺序)。您可能希望对输出进行排序以生成可预测的列表:

from operator import itemgetter

data = [{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()]
data.sort(key=itemgetter('file_name'))
json.dumps(data)

这会产生:

>>> data = [{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()]
>>> data.sort(key=itemgetter('file_name'))
>>> json.dumps(data)
'[{"file_name": "20090209.02s1.1_sequence.txt", "file_information": [645045714, 3559.6422951221466, 206045184]}, {"file_name": "20090209.02s1.2_sequence.txt", "file_information": [645045714, 3543.8322949409485, 234618880]}]'
>>> print json.dumps(data, indent=4)

[
    {
        "file_name": "20090209.02s1.1_sequence.txt",
        "file_information": [
            645045714,
            3559.6422951221466,
            206045184
        ]
    },
    {
        "file_name": "20090209.02s1.2_sequence.txt",
        "file_information": [
            645045714,
            3543.8322949409485,
            234618880
        ]
    }
]

【讨论】:

  • 嗯,我想添加那些名为 file_name 和 file_information 的键。
  • 我觉得你在这里漏了点,dict格式和json格式不一样:)
  • 是的,这就是我想要的。 +1
  • 我赞成,因为它是与 lcfseth 相同的解决方案,但一切都在一条线上?真的吗?
猜你喜欢
  • 2023-03-10
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多