I'm trying here to import CSV file which has data in just row 2 and I want to save it in a dictionary-based in it title mentioned in row 1.
My csv looks like this,
R1C1:
name,task1,task2,task3
Row2Column1:
dave,allocation,field,supervision
and, if I have more tasks in Row2Column1
, it should name task4
for the fourth task if I give. (eg):
R1C1:
name,task1,task2,task3
Row2Colum1:
dave,allocation,field,supervision,manage
(manage task should be automatically named to 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)
Actual result:
[['dave', 'allocation']]
Expected:
{name: 'dave', task1: 'allocation', task2: 'field', task3: 'supervision', task4: 'manage'}}