in Education by
I want to save files for each result from the loop. For example, I wrote the code but it only saves one file which is named "prac10.csv" which contains final results ([20, 12, 69, 31, 88, 40, 18, 71, 93, 19]). epoch=10 randomlist=[] for i in range (epoch): a=randint(1,100) randomlist.append(a) print(randomlist) with open('prac'+str(epoch)+'.csv','w') as f: f.write(str(randomlist)) However, I want to save the prac1.csv ~ prac10.csv with each loop results. prac1.csv : [20] prac2.csv : [20, 12] prac3.csv : [20, 12, 69] ..... prac10.csv : [20, 12, 69, 31, 88, 40, 18, 71, 93, 19] How do I modify my code? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You are using the fixed variable file name 'prac'+str(epoch)+'.csv' and you are overwriting your file on each iterate, so you need to use a iteration variable for your filename (i in your example): from random import randint epoch = 10 randomlist = [] for i in range(epoch): a = randint(1, 100) randomlist.append(a) print(randomlist) with open('prac'+str(i+1)+'.csv', 'w') as f: f.write(str(randomlist)) Improve your knowledge in data science from scratch using Data science online courses If you wish to learn more about Python, visit the Python tutorial and Python course by Intellipaat.

Related questions

0 votes
    I am trying to write my below pinging script results into the Text file, but I am getting an error message. ... object is not iterable Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    How do I use the for loop to execute the following operation? df_1=pd.concat([df_ab1,df_xy1], axis=1) df_2= ... Am I missing something? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    please see my code below, example_list = [ ['a','b','c'], ['f','g','h'], ['i','j','k'], ] my_string = ''' ' ... g','h'), ('i','j','k'); Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is better than R for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is necessary for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is good for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is enough for Data Science? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    Do I need coding skills for Data Science using Python? Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what should I learn in Python for Data Science? Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me where can I learn Python for Data Science? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how do I start learning Python for Data Science? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is sufficient for Data Science? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how Python is better for Data Science compared to R? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...