我现在正在做这个任务,我必须把它转换成
input csv file
下面是我的代码,它当前正在我的文件夹中创建csv文件,但实际上它没有在其中写入任何内容。在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)