如何从python中的字典创建一个字典

如何从python中的字典创建一个字典

本文介绍了如何从python中的字典创建一个字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Date        Subject     Maths  Science English French  Spanish German
16:00:00    Uploaded      100    95      65      32      23      45
17:00:00    Unknown        45    53      76      78      54      78
18:00:00    Captured       43    45      56      76      98      34

Date        BoardType     Maths  Science English French  Spanish German
16:00:00     CBSE          50     95      65      32      23      45
17:00:00     NTSE          45     53      76      78      54      78
18:00:00     YTTB         100     45      56      76      98      34

我在我的文本文件中有这两个表,名为dataVal.txt。

I have these 2 tables in my text file called dataVal.txt.

我希望输出如下: -

I want the output to be like:-

'主题':'已上传':'16: 00:00':''Maths':'100',Science :: 95 ....这样的东西。
基本上主题是第一个表的主键,其中已上传为其值,然后已上传成为具有16:00:00作为其值的键,然后这将成为关键,并拥有数学,科学,英语等作为其价值观,具有自己的价值100,95,65等等。

'Subject':'Uploaded':'16:00:00':''Maths':'100', Science::95....something like this.Basically 'Subject' is the main key for the first table which which has 'Uploaded' as its value and then 'Uploaded' becomes the key which has '16:00:00' as its value and then this becomes the key and has Maths, science, english and so on as its values which furthur have their own values as 100, 95,65 and so on.

dic = dict()
with open('C:\\Users\\aman.seth\\Documents\\dataVal.txt','r') as fh:
    for l in fh.readlines():
        try:
            lines = l.split('\t')
            date, sub, num = lines[0], lines[1], [str(x) for x in lines[2:]]
            dic.setdefault(sub, {})
            dic[sub][date] = num
        except Exception as er:
            print er
print dic

这是什么我已经做到了这一点,但我猜不够准确。

This is what I have done so far but it is not enough and accurate I guess.

推荐答案

请尝试一下,然后修正:

Please try this and lets fixed:

import re

dic = dict()

with open('txt', 'r') as fh:
    memory = None
    for line in fh.readlines():
        lines = line.rstrip('\n')
        if line.split():
            try:
                match = re.search('(BoardType|Subject)', line)
                if match:
                    memory = match.group(1)
                    dic.setdefault(memory, {})
                    header = line.split()
                    mark_index = header[2:]
                else:
                    mark_dict = dict()
                    lines = [ x for x in line.split(' ') if x]
                    date, sub, num = lines[0], lines[1], [str(x) for x in lines[2:]]
                    dic[memory].setdefault(sub, {})
                    mark = dict(zip(mark_index, num))
                    dic[memory][sub][date] = mark
            except Exception as error:
                print 'Error: ', error
import pprint
pprint.pprint(dic)

输出:

{'BoardType': {'CBSE': {'16:00:00': {'English': '65',
                                     'French': '32',
                                     'German': '45',
                                     'Maths': '50',
                                     'Science': '95',
                                     'Spanish': '23'}},
               'NTSE': {'17:00:00': {'English': '76',
                                     'French': '78',
                                     'German': '78',
                                     'Maths': '45',
                                     'Science': '53',
                                     'Spanish': '54'}},
               'YTTB': {'18:00:00': {'English': '56',
                                     'French': '76',
                                     'German': '34',
                                     'Maths': '100',
                                     'Science': '45',
                                     'Spanish': '98'}}},
 'Subject': {'Captured': {'18:00:00': {'English': '56',
                                       'French': '76',
                                       'German': '34\n',
                                       'Maths': '43',
                                       'Science': '45',
                                       'Spanish': '98'}},
             'Unknown': {'17:00:00': {'English': '76',
                                      'French': '78',
                                      'German': '78\n',
                                      'Maths': '45',
                                      'Science': '53',
                                      'Spanish': '54'}},
             'Uploaded': {'16:00:00': {'English': '65',
                                       'French': '32',
                                       'German': '45\n',
                                       'Maths': '100',
                                       'Science': '95',
                                       'Spanish': '23'}}}}

这篇关于如何从python中的字典创建一个字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:45