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:
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)