Python Forum
Automating to run python script 100 times by changing parameters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automating to run python script 100 times by changing parameters
#1
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)
Reply
#2
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  changing Python files to .exe alok 2 228 Jul-20-2021, 02:49 PM
Last Post: alok
  Changing Index of 2 List in python giddyhead 0 418 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  Help with Creating a Script for Automating Reports SunWers 1 498 Dec-29-2020, 10:21 PM
Last Post: jjc385
  Changing to new Python formatting example leodavinci1990 3 738 Sep-22-2020, 07:36 PM
Last Post: yaythomas
  Python Class and Object Parameters JoeDainton123 5 1,108 Sep-02-2020, 10:43 AM
Last Post: JoeDainton123
  How to kill a bash script running as root from a python script? jc_lafleur 4 1,399 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  changing animation speed using buttons in python microwave 1 695 Jun-24-2020, 05:21 PM
Last Post: GOTO10
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 825 May-28-2020, 05:27 PM
Last Post: micseydel
  Execute script in IDLE with parameters Pavel_47 11 2,521 Mar-14-2020, 05:41 AM
Last Post: Larz60+
  Automating Windows GUI applications metro17 4 3,669 Feb-10-2020, 09:46 AM
Last Post: metro17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020