无法在新fi中生成拆分值

2024-04-20 04:43:55 发布

您现在位置:Python中文网/ 问答频道 /正文

结果文件显示年份0
例如1985 0 0 0, 1986 0 0等等。 它不是从给定的输入文件中获取实际值。我不明白,我哪里做错了。我在.txt文件中有5个特定值,例如文件名、年份、平均最大值、平均最小值和平均降水量。你知道吗

中的数据YearlyAvarage.txt文件地址:

USC00339312.txt     1985        14.11       3.96        100.67         
USC00339312.txt     1986        15.09       5.13        94.76   
USC00339312.txt     1987        15.66       5.28        81.15   
USC00339312.txt     1988        15.22       3.66        89.82   
USC00339312.txt     1989        14.48       3.98        101.83  

我的代码:

import glob
import os
import sys


script_dir = os.path.dirname(os.path.realpath('__file__'))
path = os.path.join(script_dir[0:-3],'YearlyAverages.txt')
path1 = os.path.join(script_dir[0:-3],'YearHistogram.txt')
file_list = glob.glob(os.path.join(os.getcwd(), "wx_data", "*.txt"))
filelist = []

final_output = open('YearHistogram.txt', 'w')
for files in sorted(file_list):
    filelist.append(files.split('/')[8])


file = open('YearlyAverages.txt','r')
lines = file.read().splitlines()
row_sets = [[c for c in line.split()] for line in lines]
# print(row_sets)
data=[]

for f in filelist:
    subset = [row for row in row_sets if row[0] == str(f)]
    data.append(subset)

diction = {}

for dd in data:
    maxT = -sys.maxsize
    minT = sys.maxsize
    total = 0
    diction[dd[0][0]] = {}
    for result in dd:
        #print(result)
        if maxT < float(result[2]):
            maxT = max(maxT, float(result[2]))
            #print(maxT)
            tempMax = result[1]
            #print(tempMax)
        if minT > float(result[3]):
            minT = min(minT, float(result[3]))
            tempMin = result[1]
            #print(tempMin)
        if total < float(result[4]):
            total = max(total, float(result[4]))
            tempTotal = result[1]
            #print(tempTotal)
    diction[dd[0][0]]['max'] = tempMax
    diction[dd[0][0]]['min'] = tempMin
    diction[dd[0][0]]['total'] = tempTotal
    #print(diction)

for year in range(1985, 2015):
    maxtotal = 0
    mintotal = 0
    prectotal = 0
    for v in diction.values():
        if v['max'] == str(year):
            maxtotal = maxtotal + 1
        if v['min'] == str(year):
            mintotal = mintotal + 1
        if v['total'] == str(year):
            prectotal = prectotal + 1
    final_output.write(str(year) + '\t' + str(maxtotal) + '\t' + str(mintotal) + '\t' + str(prectotal) + '\n')

Tags: pathintxtforifosresultfloat