pythoncsv

How to iteratively run a Python script on the contents of a CSV file


I have a Python program that takes a url as a runtime argument. I want to modify this by creating a 'wrapper' to process a CSV file containing a list of urls as the input instead of a single url. The script should be executed once for each row in the CSV file.

Here is my simple script 'myscript.py':

#! /usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('url', help='input URL')

args = parser.parse_args()

print('This is the argument')
print(args.url)

which I run with python3 myscript.py https://www.bbc.co.uk

and it outputs:

This is the argument
https://www.bbc.co.uk

With a CSV file 'urls.csv' containing a list of utls, one per line, I want to be able to run something like: python3 myscript.py urls.csv The script should run the necessary number of times to produce output based on the number of urls in the file urls.csv to produce (for example):

This is the argument
https://www.bbc.co.uk
This is the argument
https://www.itv.com
This is the argument
https://www.channel4.com
This is the argument
https://www.channel5.com

I prefer a 'wrapper' approach rather than modifying the 'argparse' command in my existing script. This is in a Windows environment.


Solution

  • I could not get a satisfactory result purely in Python so I reverted to Autohotkey (ahk) to provide a "wrapper".

    #Persistent
    SetWorkingDir %A_ScriptDir%
    
    ProcessCSV(filePath) {
        if !FileExist(filePath) {
            MsgBox, CSV file not found: %filePath%
            ExitApp
        }
        FileRead, csvContent, %filePath%
        Loop, Parse, csvContent, `r, `n
        {
            line := A_LoopField
            url := Trim(line)
            if (url != "")
            {
    ; %ComSpec% /k keeps the cmd window open! Remove to run silently.
                RunWait, %ComSpec% /k python.exe myscript.py "%url%"
            }
        }
    }
    ; Get CSV file
    FileSelectFile, selectedFile, 3,, Select a CSV file, CSV Files (*.csv)
    if (selectedFile = "")
    {
        MsgBox, No file selected.
        ExitApp
    }
    ProcessCSV(selectedFile)
    ExitApp