0

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?

FabioSpaghetti
  • 790
  • 1
  • 9
  • 35
ilexcel
  • 895
  • 8
  • 12

2 Answers2

1

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']}
Demont Zhang
  • 184
  • 4
0

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
                  )
Buckeye14Guy
  • 831
  • 6
  • 12
  • So, how to separate for Masterfolder which is there in "A2" – ilexcel Aug 05 '19 at 14:20
  • I don't understand what you mean by separate Masterfolder. This gives the output you wanted though. A2 has 'Training; not Masterfolder. The only reason why I am returning a list of dictionaries is because I am assuming you have a csv file with many rows. – Buckeye14Guy Aug 05 '19 at 14:21
  • I mean when i add a variable like this: masterfolder = mydict['Master folder'] am getting error: TypeError: list indices must be integers or slices, not str – ilexcel Aug 05 '19 at 14:26
  • Where I should get the list of items from that keyword right? – ilexcel Aug 05 '19 at 14:29
  • Yea you should take the first element from the list. I mean your code is on a file with a single row or many rows? I added to the answer. `masterfolder = mydicts[0].get('Master folder')` please use the latest code. I changed `mydict` to `mydicts`. If you had more than 1 row and you wanted the master folder for the second row you could just do `masterfolder_row2 = mydicts[1].get('Master folder')` etc. – Buckeye14Guy Aug 05 '19 at 14:31
  • ya, but isn't there a way to assign all the items to a variable using keyword? – ilexcel Aug 05 '19 at 14:33
  • I'm basically trying this: masterfolder = mydicts['Master folder'] TypeError: list indices must be integers or slices, not str – ilexcel Aug 05 '19 at 14:36
  • and I am saying `mydicts` is not a dictionary. it is a list :). I mean think about it. If you have more than 1 row, the dictionary you have will not work – Buckeye14Guy Aug 05 '19 at 14:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197502/discussion-between-ilexcel-and-buckeye14guy). – ilexcel Aug 05 '19 at 14:37
  • To simply use one dict comprehension as opposed to a list comprehension you can use this answer https://stackoverflow.com/questions/23983908/multiple-key-value-pairs-in-dict-comprehension – Buckeye14Guy Aug 05 '19 at 14:49
  • yes the new update should work. I am just not using a dictionary comprehension – Buckeye14Guy Aug 05 '19 at 15:00