By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

How to loop to make many .csv files?

P: 27
Hey guy!
I have a couple questions, which I don't think are too advanced. I created an algorithm called "Best" for simplicity I will not include the mathematical details of it. I ask the user to input 6 values: Stock, min,win,Stop,GO, and end. Looking at the last 5 inputs, they are all integer inputs eg: 1,2,3.
This code will take my algorithm and spit out a csv file called "boom.csv" with two columns of numbers based on the input values. My questions are as follow:

-Can a loop be made so i input 3 integers for each user input, then spit out one .csv file for each combination of user input values?
I know I would need to some how create a way to name the .csv files for each combination of user inputs, but how would I do that?
Also how would I make a loop that can look at each combination of the user input values?
So with 3 integers per 5 input values, that should be 3^5 outputs.

Thank you for any help!
Ron Parker

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import  numpy
  3. import csv
  7. def Best(seq):
  8.     ALGORITHM
  10. Stock=raw_input('Stock Symbol: ' )
  12. min=raw_input('Start time (min away from open) : ' )
  13. win=raw_input('Window (min): ' )
  14. Stop=raw_input('SL: ' )
  15. Go=raw_input('PL: ' )
  16. end=raw_input('End time (min away from open) : ' )
  19. Entry=[ Best(something1[ i:i+120]) for i in range(0, len(something1), 120)]
  20. Entry2=[float(item[0]) for item in Entry]
  21. Entry3=[float(item[1]) for item in Entry]
  25. myFile= open( "Boom.csv", "wb" )
  26. wtr= csv.writer( myFile )
  27. for Start, Exit, in zip(Entry2,Entry3):
  28.     aRow=Start
  29.     bRow=Exit
  30.     cRow=[aRow,bRow]
  31.     wtr.writerow(cRow)
  33. myFile.close()
Jan 11 '11 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,851
I think you need a way to calculate all the possible permutations. A function that returns the permutations can be found here.

If you have a list of letters, a file name can be constructed thus:
Expand|Select|Wrap|Line Numbers
  1. >>> ext = "csv"
  2. >>> seq = ['a','b','c','d','e']
  3. >>> '.'.join(["".join(seq), ext])
  4. 'abcde.csv'
  5. >>> 
Expand|Select|Wrap|Line Numbers
  1. >>> "%s.%s" % ("".join(seq), ext)
  2. 'abcde.csv'
  3. >>> 
Jan 12 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.