从文本文件创建垂直字典

时间:2014-12-08 21:05:46

标签: python dictionary

好的,所以我有一个文本文件,前两行应该被跳过,第三行,应该是字典的键,然后我有大约15,000个条目,我需要创建一个第三行中提到的键的字典,所以它将是一个垂直字典,参数由制表符('\t')分隔。

我有:

Not important, this line should be skipped
Not important, this line should be skipped
Name Surname Telephone Address WebAddress
A1 B1 8484582 fghjkjdjstr www.fghjk.com
A2 B2 8484582 fghjkjdjstr www.fghjk.com
A3 B3 8484582 fghjkjdjstr www.fghjk.com
A4 B4 8484582 fghjkjdjstr www.fghjk.com
A5 B5 8484582 fghjkjdjstr www.fghjk.com
A6 B6 8484582 fghjkjdjstr www.fghjk.com

如何获得以下输出?

Dict = {'Name': ['A1', 'A2', 'A3', 'A4', 'A5', 'A6'],
        'Surname': ['B1', 'B2', 'B3', 'B4', 'B5', 'B6'], 
        'Telephone': ['fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr'],
        'Address': ['fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr', 'fghjkjdjstr'], 
        'WebAddress': ['www.fghjk.com', 'www.fghjk.com', 'www.fghjk.com', 'www.fghjk.com', 'www.fghjk.com', 'www.fghjk.com']}

我发布了我的尝试

def Reading_Old_File(self, Path):
        Old_Values={}
        with open(Path,'r') as Old_File:
            while line:
                for index, line in enumerate(Old_File, start=1):
                    if index==1:
                        pass
                    if index==2:
                        pass
                    if index==3:
                        indexes=line.split("\t")
                        for index in indexes:
                            Old_Values=Old_Values{[key]} # What here?
                    if index>3:
                        data=line.split("\t")
            print Old_Values

正如我之前提到的,前两行有不重要的信息,第三行应该是键,第四行和下一行是值,所以我想要附加值,键和字典,这可能吗?

2 个答案:

答案 0 :(得分:1)

if index==3:
    indexes=line.split("\t")
    for index in indexes:
         Old_Values=Old_Values{[key]} # What here?

据我了解,您想要的只是一个空列表,用于放置后续行中的相应元素:

Old_Values[index] = []

哪个会给你:

{'WebAddress': [], 'Address': [], 'Surname': [], 'Name': [], 'Telephone': []}

我不知道key来自何处,并且您的函数无法启动,因为while line在定义line之前发生(并且无论如何都是多余的)。真的,它应该看起来像:

def reading_old_file(self, path):
    old_values = {}
    with open(path) as old_file: # 'r' is the default mode
        for index, line in enumerate(old_file, start=1):
            if index == 3: # just leave out pass cases 
                indexes = line.split("\t")
                for index in indexes:
                    old_values[index] = []
            elif index > 3: # use elif for exclusive cases 
                data = line.split("\t")
                ... # use indexes to put elements in appropriate lists
    print old_values # should probably return something, too

请注意遵守style guide

答案 1 :(得分:0)

这样的事可能有用:

data = {}
headers = None
for line in open('file.txt'):
    cols = line.strip().split()
    if not headers:
        headers = cols
        continue
    for field, c in zip(headers, col):
        data.setdefault(field, []).append(c)