Python Forum
Looping script and writing to Excel in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping script and writing to Excel in Python
#1
m used to program in statistical programs like Stata and R but I’ve recently picked up on Python and I’m trying to teach myself. One thing that puzzles me is perhaps a simple (or maybe complex, I don’t know) is how to loop my scripts and write the result of each script to an excel file. I’ve been playing with this script that is heavily inspired by scripts that does the same (just to learn); the infinite monkey script, where I try to simulate a monkey on a typewriter trying to guess words that I define. I’ve succeeded in making a script that runs..... somewhat okay with shorter words (words above 7 letters takes quite some time) but I would like to take the result, write it to an excel-file and rerun to do the same estimate maybe 100 times more and writing the last line to the excel-file. Okay, enough chat. This is the code I’d like to loop (sorry for the bad formatting - something is being weird when I copy-paste the code):

import numpy as np

class Best:
    def __init__(self):
        self.iBestScore = 0
        self.sBestGuess = None

    def getScore(self):
        return self.iBestScore

    def getGuess(self):
        return self.sBestGuess

    def setScore(self, new):
        self.iBestScore = new

    def setGuess(self, new):
        self.sBestGuess = new

def randString(iStrLen):
    sGuess = np.random.randint(97, 123, iStrLen,  dtype=np.uint8).tostring().decode('ascii')
    return sGuess

def goalCheck(sGiven, sGuess, iStrLen, best):
    iScore = 0
    for i in range(iStrLen):
        if sGuess[i] == sGiven[i]:
            iScore += 1
    if(iScore >= best.iBestScore or best.iBestScore == 0):
        best.iBestScore = iScore
        best.sBestGuess = sGuess
    return best

#@profile
def begin(sGiven):
    best = Best()
    iTries = 0
    iBestScore = 0
    sGuess = randString(len(sGiven))
    iTries += 1
    best = goalCheck(sGiven, sGuess, len(sGiven), best)
    while(best.iBestScore != (len(sGiven))):
        sGuess = randString(len(sGiven))
        iTries += 1
        best = goalCheck(sGiven, sGuess, len(sGiven), best)
        if iTries % 100000 == 0 and iTries != 0:
            print("Tries: %d Best Score: %d Best Guess: %s" %(iTries, best.iBestScore, best.sBestGuess))
    return best.sBestGuess, iTries

def main():
    print("Done! Found '{:s}' in {:d} tries.".format(*begin("hat")))

if __name__ == "__main__":
    main()
Reply
#2
Gentle bump. Anyone?
Reply
#3
Have you attempted writing to csv and it failed? If so, you can show us code and errors you got.
If not, there are tons of examples online on how to write to csv file.
Python documentation
A tutorial
Another tutorial

Just search for csv writing sections of tutorials/examples. Or specifically csv.writer method.

If you get stuck, show us the code, actual vs. desired output, and potential error messages, and we will look into it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas writing to excel .. help anna 0 645 Jun-20-2019, 06:34 AM
Last Post: anna
  panda, excel - script pauses and doesn't continue running, no error message william 1 928 Nov-24-2018, 01:24 AM
Last Post: ichabod801
  looping in python jazzy 12 2,567 Aug-27-2018, 05:17 PM
Last Post: buran
  Pandas/Excel, reading from one column writing back to another... williamlombard 1 1,092 Mar-01-2018, 06:02 AM
Last Post: williamlombard
  Trouble with creating a looping function in python 2.7 (complex) Afterdarkreader 3 1,389 Dec-12-2017, 06:56 PM
Last Post: Afterdarkreader

Forum Jump:

User Panel Messages

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