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
  Automating the CyberGhost VPN surendragupta 0 757 Apr-30-2025, 11:10 AM
Last Post: surendragupta
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 1,941 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Virtual Env changing mysql connection string in python Fredesetes 0 1,462 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
Question Is there a python program for automating this invoicing workflow? PG_Archipelago 3 3,271 Feb-02-2023, 11:01 PM
Last Post: Larz60+
  Saving the times a script is run to a file or ... 3Pinter 7 4,318 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 9,042 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  changing Python files to .exe alok 2 3,436 Jul-20-2021, 02:49 PM
Last Post: alok
  Changing Index of 2 List in python giddyhead 0 2,433 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  Help with Creating a Script for Automating Reports SunWers 1 3,212 Dec-29-2020, 10:21 PM
Last Post: jjc385
  Changing to new Python formatting example leodavinci1990 3 3,357 Sep-22-2020, 07:36 PM
Last Post: yaythomas

Forum Jump:

User Panel Messages

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