ilexcel
ilexcel

Reputation: 987

Problem with getting data from csv using pythonand writing them to a dictionary

I have a CSV in a below format:

enter image description here Expected: Where I want to convert this to dictionary like this:

{'Masterfolder': ['Training'], 'Childfolder': ['Training videos', 'Training documents', 'Training workouts', 'Training practicals']}

So far I have done the following code,

import csv

with open('features.csv', mode='r') as f:
reader = csv.reader(f)
checker = lambda i: bool(i and i.strip()) 
mydict = {rows[0]: list(filter(checker, rows[0:])) for rows in reader}

print(mydict)

And my output is something like this:

{'Master folder': ['Child - 1', 'Child - 2', 'Child - 3', 'Child - 4'], 'Training': ['Training videos', 'Training documents', 'Training workouts', 'Training practicals']}

How can I improve this code to get in order to get the result as I am expecting?

Upvotes: 0

Views: 57

Answers (2)

Buckeye14Guy
Buckeye14Guy

Reputation: 851

This is what I mean by separating the header. The first element is your header so using next to get that out is best. Then you can work on the other rows.

with open(r'./data/temp.csv', 'r') as f:
    checker = lambda i: bool(i and i.strip())
    reader = csv.reader(f)
    header = next(reader)
    row = next(reader)
    mydict = {
        header[0]: [row[0]],
        'Childfolder': list(filter(checker, row[1:]))
    }

Or this way if you don't want to store the row at all

with open(r'./data/temp.csv', 'r') as f:
    checker = lambda i: bool(i and i.strip())
    reader = csv.reader(f)
    header = next(reader)
    mydict = next({
                      header[0]: [row[0]],
                      'Childfolder': list(filter(checker, row[1:]))
                  } for row in reader
                  )

Upvotes: 0

Demont Zhang
Demont Zhang

Reputation: 224

You can use Pandas

Read your CSV file with pd.read_csv and change the index

import pandas as pd 

df = pd.read_csv('Classeur1.csv', sep=';', index_col='Master folder')

output :

                   Child - 1          Child - 2          Child - 3           Child - 4
Master folder               
Training           Training videos    Training document Training workouts   Training praticals

Then make your dict

mydict = {'Master folder' : list(df.index),
          'Childfolder' : list(df.iloc[0])}

output :

{'Master folder': ['Training'],
 'Childfolder': ['Training videos','Training document','Training workouts','Training praticals']}

Upvotes: 1

Related Questions