我正在尝试导入仅在第2行中包含数据的CSV文件,我想将其保存在第1行中提到的基于字典的标题中。
我的csv看起来像这样,
R1C1:
name,task1,task2,task3
Row2Column1:
dave,allocation,field,supervision
,并且,如果我在Row2Column1
中还有更多任务,则我应该给第四项任务命名为task4
。 (例如):
R1C1:
name,task1,task2,task3
Row2Colum1:
dave,allocation,field,supervision,manage
(管理任务应自动命名为task4
)
import csv
path = "C:\\tasks.csv"
file = open(path, newline='')
reader = csv.reader(file)
header = next(reader)
data = []
for row in reader:
name = row[0]
tasks = row[1]
data.append([name, tasks])
print(data)
实际结果:
[['dave', 'allocation']]
预期:
{name: 'dave', task1: 'allocation', task2: 'field', task3: 'supervision', task4: 'manage'}}
答案 0 :(得分:0)
正如@Grismar所说,当您应该改用#include <stdio.h>
#include <math.h>
int main()
{
float x=2;
float y=2;
float p= pow(x,y);
printf("%f",p);
return 0;
}
时,您正在list
上附加data
...
默认情况下,csv阅读器将一行作为列表读取。因此,您具有以下条件:
dict
如果要添加字典,可以将这两个列表合并为一个,如此处所示: How do I combine two lists into a dictionary in Python?
您的代码应具有以下内容:
header = ['name', 'task1', 'task2', 'task3']
tasks = [ <values> ]