Is it possible to write a loop that includes variables from a list?

Hi all, please bear with me as i’m fairly new to Python. I have a working python script that does the following:

  • Opens a csv file
  • Edits the csv (removes first row, adds a column, data-fills column)
  • Exports the csv with new name
    The source csv, new column name, and exported csv name are defined by variables.

My goal is to build some kind of loop that replaces these three variables from a lookup list instead of copying and pasting the script 200 times. Here is a sample from my lookup list (csv file) and my python script.

Is it possible to replace my three variables in the python script and have a loop that iterates through a lookup list instead?

Lookup List:
image

Python Script:

import pandas as pd
import os, shutil
import numpy as np
import csv

# Define source and export directories
sdir = 'E:/Python Projects/ParmAudit/Dump/'
edir = 'E:/Python Projects/ParmAudit/Export/'



# Item A

# Define variable names
sourcefile = 'N_1.txt'
pname = 'itemA'
ename = 'itemA.csv'

# Remove first row
with open(os.path.join(sdir, sourcefile)) as f:
    lines = list(f)

lines.pop(0) # Delete first row


with open(os.path.join(sdir, sourcefile), "w") as f:
    for line in lines:
        f.write(line)

# Create dataframe from newly saved file
DF = pd.read_csv(os.path.join(sdir, sourcefile))
# Create new column and datafill rows as pname
DF['Parm'] = pname
# Move new column to first slot
first_column = DF.pop('Parm')
DF.insert(0, 'Parm', first_column)
# Export datagrame as csv named as ename
DF.to_csv(os.path.join(edir, ename), index=False)

Not too sure if I have a handle on this, but if I have, then generating the pname and ename in a loop, is relatively straightforward:

alpha = [chr(n) for n in range(65, 91)] # A to Z array

for letter in alpha:
    pname = f'item{letter}'
    print(pname)  # just to show it
    ename = f'{pname}.csv'
    print(ename)  # just to show it
    print()

If you already have the lookup list saved in a CSV file with the column header row, then you can read from it like this:

import csv

lookup_list_path = 'path/to/lookup list'

with open(lookup_list_path) as lookup_file:
    for row in csv.DictReader(lookup_file):
        sourcefile = row['sourcefile']
        pname = row['pname']
        ename = row['ename']

        # Do stuff here to process the CSV file.
1 Like

This worked perfectly. Thanks so much for your help!!! I’ve been fighting this for days lol.