python3 csv 模板_用python3编写csv文件

我现在正在做这个任务,我必须把它转换成

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值