Home > Net >  How can I write the value of k for periods from 0 to 50 into a csv-file?
How can I write the value of k for periods from 0 to 50 into a csv-file?

Time:11-08

I wrote this code:

delta, s, theta = 0.08, 0.3, 0.35
k = 2.5
for i in range(1,51):
    k = (1 - delta) * k   s * k ** theta
    print(f"{i:3d}: {k}")    

import csv
with open('capital.csv', 'w', newline='') as csvfile:
     fieldnames = ['col1', 'col2']
     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
     writer.writeheader()
     for a, b in range(i,k)
         writer.writerow({'col1': a, 'col2': b})

I would like to see in the Excel document the value of i from 0 to 50 in the first column and the value of k in the second column.

CodePudding user response:

if I get it right, you want 2 columns. one is the range of numbers from 1 to 51 and the second one is the calculated value (k) this would make it happen:

import csv

delta, s, theta = 0.08, 0.3, 0.35
k = 2.5
with open('capital.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['col1', 'col2'])
    writer.writerow([0, k])
    for i in range(1, 51):
        k = (1 - delta) * k   s * k ** theta
        writer.writerow([i, k])
  • Related