将您的键值对拆分为单独的字典:
json.dumps([{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()])
请注意,您的输出顺序是任意的(字典没有固定的顺序)。您可能希望对输出进行排序以生成可预测的列表:
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
]
}
]