Search code examples
pythonglob

Python perform logic for all CSV files


I've got the following python script that opens a single CSV file and performs a few bits to recreate columns and slices lists, finally outputs to CSV.

I would like the script to one all current .CSV files in a directory and perform the actions and output a final CSV file.

FYI: I'm trying to avoid PANDAS.

import csv

projects = []
count = 0 
with open('timesheets.csv') as csvfile:
    timesheets = csv.reader(csvfile, delimiter=',')
    for rows in timesheets:
        # IF statement will run until count is more than 3, then move out of IF statement and print remaining rows.
        if count < 3:
            count += 1
            continue
        # Columns 1,8,9,13,15,18
        columns1 = rows[1:2]
        columns8_9 = rows[8:10]
        columns13 = rows[13:14]
        columns15 = rows[15:15]
        columns18 = rows[18:19]
        project = columns1 + columns8_9 + columns13 + columns15 + columns18
        # Append each line as a seperate list to projects list. You end up with multiple lists within in a list.
        projects.append(project)



# Remove the last list in projects list, since being empy and causes errors.
projects = projects[:-1]

newlist = []
# Remove the first 6 characters from each line[1] & line[4]
for lists in projects:

    # Remove the first 6 characters from each line[1]
    engineer = lists[1]
    engineer = engineer[8:]
    lists[1] = engineer

    # Remove the first 6 characters from each line[3]
    employee = lists[3]
    employee = employee[8:]
    lists[3] = employee

    newlist.append(lists)

# Change the first list to the following list, which effectively changes the column names.
newlist[0] = ['Project Name', 'Line Manager', 'Element', 'Employee', 'Hours']


writer = csv.writer(open('output.csv', 'w'))
writer.writerows(newlist)

Solution

  • Put list of csv's in a list and iterate. This will help you:-

    import glob
    files_list = glob.glob("path/*.csv")
    for file in files_list:
        #your remaining code goes here...