Search code examples
pythonpython-3.xcsvdictionarypruning

How do I prune all the '' from these strings generated from a CSV?


I've made this formula which turns a CSV into a dictionary:

def CSVtoDict(BDF, mode):
    saved={}
    with open('%s%s.csv' % (dataDir,BDF), mode='r') as infile:
        reader = csv.reader(infile)
        for row in reader:
            if mode is 'prune' and row == '':
                break
            else:
                pass
            key = row[0]
            saved[key]=row[1:]
    return saved

I'm trying to make it so that when the mode is 'prune', any string from the CSV such as ['data1', 'data2', '', '', ''] is returned without any ''. But the break statement doesn't seem to work for some reason and things are returned as if there was no if else.

Also do you know what would be the pythonic way of doing this? I feel like this could be more efficient somehow...

Example line from the CSV:

Awareness,ASD,ASD2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,


Solution

  • I think the problem is that you still have to loop through each entry, not just each row. Here is my solution:

    # CSV contents
    #  'data1', 'data2', 'data3', ''
    #  'data4', 'data5', '' ''
    #  'data6', '', '', ''
    
    def CSVtoDict(BDF, mode):
        saved={}
        with open('%s%s.csv' % (dataDir,BDF), mode='r') as infile:
            reader = csv.reader(infile)
            for row in reader:
                key = row[0]
                saved[key] = [r for r in row[1:] if not (r is '' and mode is 'prune')]
        return saved
    
    
    # saved will equal
    # {
    #   'data1': ['data2', 'data3']
    #   'data4': ['data5'], 
    #   'data6': [], 
    # }