将制表符分隔的文件转换为字典(python)

1 投票
2 回答
3445 浏览
提问于 2025-04-16 13:25

我有一个用制表符分隔的文件。

我该如何把这个文件的内容放入一个字典里呢?

2 个回答

3

你可以使用 csv 模块里的 DictReader

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

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

撰写回答