I'm currently working on this assignment where I have to convert essentially this input csv file
into this written csv file
here's my code that's currently creating the csv file in my folder but it's not actually writing anything in it.
import csv
def read_calculate():
dict1 = {}
with open('sunspots.csv','r') as file:
csv_reader = csv.reader(file, delimiter=',')
for row in csv_reader:
year_month = row[0] + row[1]
if year_month in dict1:
if(row[4].isnumeric() and int(row[4])!= -1):
dict1[year_month]+= int(row[4])
else:
if(row[4].isnumeric() and int(row[4])!=-1):
dict1[year_month]=int(row[4])
else:
dict1[year_month]=0
file.close()
return dict1
def write_to_file(dict1):
with open('Monthtotal.csv','w',newline='') as write_file:
writer = csv.writer(write_file)
for key in dict1.keys():
line = key[0:4],k[4:6],str(dict1[key])
writer.writerow(line)
write_file.close()
if __name__=='__main__':
dict1 = read_calculate()
write_to_file(dict1)