我正在尝试将 *.tab 文件转换为字典。文件中有 16 列和 154 行,包括标题。应忽略标题行和 4 到 13 之间的列。字典的键是一个字符串元组,例如("老虎,猫"(
我尝试了带有代码的现有解决方案,但它显示的输出不是我预期的。这是 .tab 文件的前几行
Word 1 Word 2 Human (mean) 1 2 3 4 5 6 7 8 9 10 11 12 13
love sex 6.77 9 6 8 8 7 8 8 4 7 2 6 7 8
tiger cat 7.35 9 7 8 7 8 9 8.5 5 6 9 7 5 7
tiger tiger 10.00 10 10 10 10 10 10 10 10 10 10 10 10 10
book paper 7.46 8 8 7 7 8 9 7 6 7 8 9 4 9
computer keyboard 7.62 8 7 9 9 8 8 7 7 6 8 10 3 9
computer internet 7.58 8 6 9 8 8 8 7.5 7 7 7 9 5 9
plane car 5.77 6 6 7 5 3 6 7 6 6 6 7 3 7
train car 6.31 7 7.5 7.5 5 3 6 7 6 6 6 9 4 8
telephone communication 7.50 7 6.5 8 8 6 8 8 7 5 9 9 8 8
这是我使用的代码:
import csv
with open('set1.tab') as set1:
# skip the first line
next(set1)
list_of_dicts = list(csv.DictReader(set1, dialect='excel-tab'))
预期结果应为: {('tiger', 'cat'( : 7.35, ....}
但这是我的实际结果:
[OrderedDict([('love', 'tiger'),
('sex', 'cat'),
('6.77', '7.35'),
('9', '9'),
('6', '7'),
('8', '7'),
('7', '5'),
('4', '5'),
('2', '9'),
('', '')]),
这应该可以做到:
import csv
with open('set1.tab') as set1:
# skip the first line
next(set1)
rows = csv.reader(set1, dialect='excel-tab')
dict_data = {(fields[0], fields[1]): fields[2:] for fields in rows if len(fields) >= 2}
print(dict_data)