Applying code for one file to multiple files python (Newbie Question)

Viewed 127

I wrote the following code that takes a column from a csv file, it then converts it into an integer and adds them all up. I have done this for only one file and I have around 80 files to apply the same code to.

import csv
from collections import defaultdict
columns = defaultdict(list)
with open('Team11BoM.csv') as f:
    reader = csv.DictReader(f)
    for row in reader:
        for (k,v) in row.items():
            if k not in columns:
                columns[k] = list()
            columns[k].append(v)

import pandas as pd
df = pd.read_csv("Team11BoM.csv")

b = list(df['Reported Price'])
a = list(df['Actual Price'])

for i in range(0, len(a)):
    a[i] = int(float(a[i]))

v = sum(a)
print("the total actual cost(s) for team 11 is:", v)

for i in range(0, len(b)):
    b[i] = int(float(b[i]))

h = sum(b)
print("the total reported price for team 11 is:", h)

it prints out the following:

the total actual cost(s) for team 11 is: 945
the total reported price for team 11 is: 707

I want it to print out:

the total actual cost(s) for *filename* is: *Total cost of that team*
the total reported price for *filename* is: *Total reported price of that team*

Is there any simple way to do this?

thanks, Irfan S.

4 Answers

First, you should define a function that you can reuse to avoid code repetition.

import csv
from collections import defaultdict

def process_file(file_name):
    columns = defaultdict(list)
    with open(file_namename) as f:
        reader = csv.DictReader(f)
        for row in reader:
            for (k,v) in row.items():
                if k not in columns:
                    columns[k] = list()
                columns[k].append(v)

    import pandas as pd
    df = pd.read_csv(file_name)

    b = list(df['Reported Price'])
    a = list(df['Actual Price'])

    for i in range(0, len(a)):
        a[i] = int(float(a[i]))

    v = sum(a)
    print(f"the total actual cost(s) for {file_name} 11 is:", v)

    for i in range(0, len(b)):
        b[i] = int(float(b[i]))

    h = sum(b)
    print(f"the total reported price for {file_name} 11 is:", h)

Second, call this function and iterate over the list of files:

# assuming all of this files are in the current directory

list_of_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file_name in list_of_files:
    process_file(file_name)
import os
import csv
import pandas as pd
from collections import defaultdict

files_dir = 'csv'

csv_files = os.listdir(files_dir)
print(csv_files)

def convert_to_int(file_name):
    file_name = f'{files_dir}/{file_name}'
    columns = defaultdict(list)
    with open(file_name) as f:
        reader = csv.DictReader(f)
        for row in reader:
            for (k,v) in row.items():
                if k not in columns:
                    columns[k] = list()
                columns[k].append(v)

    df = pd.read_csv(file_name)

    b = list(df['Reported Price'])
    a = list(df['Actual Price'])

    for i in range(0, len(a)):
        a[i] = int(float(a[i]))

    v = sum(a)
    print("the total actual cost(s) for team 11 is:", v)

    for i in range(0, len(b)):
        b[i] = int(float(b[i]))

    h = sum(b)
    print("the total reported price for team 11 is:", h)

for file in csv_files:
    convert_to_int(file)

You can use for loop and iterate over each file in the cwd and do the same with all, Make sure all files are in the same directory

import csv
from collections import defaultdict
import pandas as pd
import os

def valueSum(filename):
    columns = defaultdict(list)
    with open(filename) as f:
        reader = csv.DictReader(f)
        for row in reader:
            for (k,v) in row.items():
                if k not in columns:
                    columns[k] = list()
                columns[k].append(v)

    df = pd.read_csv(filename)

    b = list(df['Reported Price'])
    a = list(df['Actual Price'])

    for i in range(0, len(a)):
        a[i] = int(float(a[i]))

    v = sum(a)

    for i in range(0, len(b)):
        b[i] = int(float(b[i]))

    h = sum(b)

    print("the total actual cost(s) for team 11 is:", v)
    print("the total reported price for team 11 is:", h)

for filename in os.listdir("."):
    if filename.endswith(".csv"): #count only csv files
        valueSum(filename)

How about placing csvs in one directory and perfom loop like this:

import pandas as pd
def summer(f):
    name = f.split('.')[0]
    df = pd.read_csv(f)

    b = list(df['Reported Price'])
    a = list(df['Actual Price'])

    for i in range(0, len(a)):
        a[i] = int(float(a[i]))

    v = sum(a)
    print(f"the total actual cost(s) for {name} is:", v)

    for i in range(0, len(b)):
        b[i] = int(float(b[i]))

    h = sum(b)
    print("the total reported price for {name} is:", h)

path = 'path/to/csv-files/directory/'

import os
for fil in os.listdir(path):
    summer(fil)

Related