马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
现在有一个文本文件,是不同站点不同年份每日的逐小时降水量,想编个程序将逐小时的降水量加和成每日的降水量,原文件和我编的程序如下,有点着急,请大佬帮忙找一下问题,万分感谢
f = open('逐小时降水.txt')
number = []
year = []
mouth = []
day = []
total = []
tp = []
count = 0.0
for each_line in f:
if each_line[:12] !="区站号":
(plot,nian,yue,ri,shi,num) = each_line.split()
if plot == 'M1151':
year.append(nian)
mouth.append(yue)
day.append(ri)
if num == '/':
num = 0.0
total.append(float(num))
for i in range(len(each_line)):
if year[i] == year[i+1] and mouth[i] == mouth[i+1] and day[i]== day[i+1]:
count = count + total[i]
else :
tp.append(count+total[i])
count = 0
file_name = '日降水量'+'.txt'
tp_file = open(file_name,'w')
tp_file.writelines(str(tp))
tp_file.close()
f.close()
试试看效果是不是你想要的那种f = open('逐小时降水.txt',encoding='utf-8')
data = []
number = 0
for each_line in f:
temp = []
if each_line[:3] != "区站号":
(plot, year, mouth, day, time, num) = [i for i in each_line.split(' ') if i!='']
if plot == 'M1151':
temp.append(year)
temp.append(mouth)
temp.append(day)
temp.append(time)
if num == '/' or num == '/\n':
num = 0.0
temp.append(float(num))
data.append(tuple(temp))
# print(data)
new_data = []
for i in range(len(data)-1):
if data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0] == data[i+1][zxsq-anti-bbcode-0] and data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1] == data[i+1][zxsq-anti-bbcode-1] and data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-2] == data[i+1][zxsq-anti-bbcode-2]:
number += data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-4]
continue
number += (data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-4] + data[i+1][zxsq-anti-bbcode-4])
new_data.append((data[zxsq-anti-bbcode-i][0:3] + tuple((str(number),'')))[:-1])
number = 0
file_name = '日降水量' + '.txt'
tp_file = open(file_name, 'w',encoding='utf-8')
tp_file.write('{:^6}{:^6}{:^6}{:^10}\n'.format('年份','月份','日期','日降水量(mm)'))
for i in range(len(new_data)):
tp_file.write('{:^8}{:^8}{:^8}{:^10.2f}\n'.format(new_data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0],new_data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1],new_data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-2],float(new_data[zxsq-anti-bbcode-i][zxsq-anti-bbcode-3])))
tp_file.close()
f.close()
|