Python 使用Dictreader以表格形式读取csv文件

Python 使用Dictreader以表格形式读取csv文件,python,csv,dictionary,Python,Csv,Dictionary,如果我有一个CSV文件,其组织如下: 我正在尝试使用Dictreader for csv模块: file = open('file.csv') csv_file = csv.DictReader(file) data = {row['model']: row for row in csv_file} 我试图将其理解为字典,其中每个模型及其系数都有一个单独的字典,如下所示: { first:{a:1,b:2,c:3,d:4}, second:{a:1,b:2,c:3,d:4}, t

如果我有一个CSV文件,其组织如下:

我正在尝试使用Dictreader for csv模块:

file =  open('file.csv')
csv_file = csv.DictReader(file)
data = {row['model']: row for row in csv_file}
我试图将其理解为字典,其中每个模型及其系数都有一个单独的字典,如下所示:

{
  first:{a:1,b:2,c:3,d:4},
  second:{a:1,b:2,c:3,d:4},
  third:{a:1,b:2,c:3,d:4},
}
但我在内部字典中仍然有型号名称。如何删除该选项?

您可以从每行dict中弹出模型键:

data = {row.pop('model'): row for row in csv_file}
您可以从每行dict中弹出模型键:

data = {row.pop('model'): row for row in csv_file}

当你说你想要模型名。。。remove[d]您是说希望输出为列表,只包含值吗?如下所示:首先:[1,2,3,4],是的,当您说需要模型名称时,此输出也是可以接受的。。。remove[d]您是说希望输出为列表,只包含值吗?这样:首先:[1,2,3,4],是的,这个输出也是可以接受的