制表符分隔文件到字典(python)

时间:2011-03-10 07:49:24

标签: python dictionary

我有一个标签分隔文件

如何将此文件输入字典?

2 个答案:

答案 0 :(得分:3)

您可以将csv模块与DictReader class一起使用。

答案 1 :(得分:3)

import csv

with open(filename) as file_object:
    # skip the first two lines
    file_object.next()
    file_object.next()
    list_of_dicts = list(csv.DictReader(file_object, dialect='excel-tab'))

# list_of_dicts now will contain a list of dictionaries extracted from the file