Python Forum

Full Version: Automating to run python script 100 times by changing parameters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All, I am trying to automate the test which I have to run 100 times by changing the data that read from csv file. I have a csv file called test-100.csv which contains 100 columns and about 2000 rows per column. My purpose is that read the data from each column in every execution of python script and save the output to different csv file name. Now I have finished for reading first column of csv file and then save it into test-automate.csv. How can I automatically run the python script by changing this line[line.split(',')[0] for line in f] into [line.split(',')[1] for line in f] and up to [line.split(',')[99] for line in f] and also file name into different file names without manually editing? Thanks for your help.


import csv
import sys
filename = open('test-automate.csv', 'w')
sys.stdout = filename
with open('test-100.csv') as f:
    firstColumn = [line.split(',')[0] for line in f]
    results = [float(i) for i in firstColumn]
    int_result = [round(i) for i in results]
    print(int_result)

filename.close()
    # reattach stdout to console
sys.stdout = sys.__stdout__
with open('test-automate.csv') as file:
    data = file.read()
    print(data)
I'm sure you have it sorted by now, but in case anyone else is looking:
Many ways to do this - I am often using pandas or numpy when these kind of circumstances arise but the easiest is:
#add following two lines:
x = 0
while x <= 100:
#indent all that follows:
#add following line:
    fn = 'test-automate'+str(x)+'.csv'
#edit following line:
    filename = open(fn, 'w')
    sys.stdout = filename
    with open('test-100.csv') as f:
#edit following line:
        firstColumn = [line.split(',')[x] for line in f]
        results = [float(i) for i in firstColumn]
        int_result = [round(i) for i in results]
        print(int_result)

    filename.close()
        # reattach stdout to console
    sys.stdout = sys.__stdout__
#edit following line:
    with open(fn) as file:
        data = file.read()
        print(data)
#add following line:
    x+=1
[/quote]