我在文本文件中有这种格式的数据。我想要的只是在字典数据结构中的字典中隐藏这些内容
('Marks_Subjects','[['Time','maths','Science','english'],['2013-08-31-16',100,50,65],['2013- 08-31-17',90,50,65],['2013-08-31-18',80,60,85]]“)
我希望输出如下: -
{'Marks': {'maths': {'2013-08-31-16': {'Subjects': '100'}},
'Science':{'2013-08-31-16': {'Subjects': '50'}},
'maths':{'2013-08-31-16': {'Subjects': '65'}}}
依旧是17:00和18:00
以下是代码段: -
import sys
import json
def module2():
title, data = ('Marks_Subjects ', "[['Time', 'maths', 'Science','english'],['2013-08-31-16', 100, 50, 65], ['2013-08-31-17', 90, 50, 65],['2013-08-31-18', 80, 60, 85]]")
title, sub_title = title.split('_')
data = json.loads(data.replace("'", '"'))
data = dict(zip(*data))
date = data.pop('Time')
#for subject, value in data.iteritems():
print {title: {subject: {date: {sub_title: value}} for subject, value in data.iteritems()}}
module2()
我将标题Marks_Subjects拆分,以便Marks是我的标题,而Subjects是我的sub_title。 json.loads将unicode字符串转换为python数据结构data.zip映射2个列表的索引。这里的问题是我收到此错误
ValueError: dictionary update sequence element #0 has length 4; 2 is required
我想这意味着zip功能只能压缩2个列表。我想将第一个列表的索引["Time", "Maths", "Science", "English"]
与其余列表压缩。
所以我想必须运行一个循环才能实现这个目标。但我需要一些帮助。
如果我的“数据”中有2个列表,那么它可以正常工作。
答案 0 :(得分:0)
title, data = ('AssetType_FrameDrops ', "[['Time', 'uploaded', 'Unknown', 'captured'], ['2013-08-31-16', 34885, 0, 943640], ['2013-08-31-17', 19167, 0, 1095645], ['2013-08-31-18', 11610, 0, 1005367], ['2013-08-31-19', 4741, 0, 1318737], ['2013-08-31-20', 5829, 0, 1066768], ['2013-08-31-21', 5748, 0, 946647], ['2013-08-31-22', 55554, 0, 900068], ['2013-08-31-23', 70560, 0, 792410]]")
key1, key2 = title.split("_")
from ast import literal_eval
data = literal_eval(data)
headers, data = data[0], data[1:]
marks = [dict(zip(headers, items)) for items in data]
result = {}
for current_dict in marks:
current_time = current_dict["Time"]
for k, v in current_dict.items():
if k == "Time": continue
if k not in result: result[k] = {}
if current_time not in result[k]: result[k][current_time] = {}
result[k][current_time][key2] = v
from pprint import pprint
result = {key1: result}
pprint(result)
<强>输出强>
{'AssetType': {'Unknown': {'2013-08-31-16': {'FrameDrops ': 0},
'2013-08-31-17': {'FrameDrops ': 0},
'2013-08-31-18': {'FrameDrops ': 0},
'2013-08-31-19': {'FrameDrops ': 0},
'2013-08-31-20': {'FrameDrops ': 0},
'2013-08-31-21': {'FrameDrops ': 0},
'2013-08-31-22': {'FrameDrops ': 0},
'2013-08-31-23': {'FrameDrops ': 0}},
'captured': {'2013-08-31-16': {'FrameDrops ': 943640},
'2013-08-31-17': {'FrameDrops ': 1095645},
'2013-08-31-18': {'FrameDrops ': 1005367},
'2013-08-31-19': {'FrameDrops ': 1318737},
'2013-08-31-20': {'FrameDrops ': 1066768},
'2013-08-31-21': {'FrameDrops ': 946647},
'2013-08-31-22': {'FrameDrops ': 900068},
'2013-08-31-23': {'FrameDrops ': 792410}},
'uploaded': {'2013-08-31-16': {'FrameDrops ': 34885},
'2013-08-31-17': {'FrameDrops ': 19167},
'2013-08-31-18': {'FrameDrops ': 11610},
'2013-08-31-19': {'FrameDrops ': 4741},
'2013-08-31-20': {'FrameDrops ': 5829},
'2013-08-31-21': {'FrameDrops ': 5748},
'2013-08-31-22': {'FrameDrops ': 55554},
'2013-08-31-23': {'FrameDrops ': 70560}}}}