尝试从文件中提取数据,然后从文件中计算两个不同的平均值,它一直调到 0.0,我该怎么办



该程序要求一个包含天气数据的文件的名称,例如rettet_verdata_florida.txt。然后,它会打开该文件,并询问您希望将特定数据写入的文件的名称。它还要求年份。

然后,它应该读取该年不同月份的数据(在文本文件中用\t分隔),将月份的总和放在"sum_temp"和"sum_wind"中,并在打印时计算平均值打印到文件和屏幕。但是每个月的平均数最终为 0.0,即使它应该工作正常。

以下是一些列表:

Stnr Dato DD06 DD12 DD18 FFM FXM POM TAM UUM50540   07.01.1957  150 170 170 6.2 8.8 1010.6  6.3 9450540   08.01.1957  160 160 200 7.2 9.8 1001.8  8.0 9950540   09.01.1957  290 200 160 8.1 13.3    990.2   5.7 9150540   10.01.1957  300 360 350 12.7    15.4    1008.2  2.8 8750540   11.01.1957  0   200 160 3.8 10.4    1015.1  1.7 9850540   12.01.1957  0   330 340 5.1 10.4    995.0   5.0 9650540   13.01.1957  350 130 60  6.5 12.3    1018.2  1.6 5450540   14.01.1957  0   130 150 3.1 7.0 1033.0  -2.8    69

这是代码:

def year_avg():
    #Asks for the file name to be opened, file name for storage, and year (buffer against error).
    file_opening = input("Write the file to be opened (with .txt): ") or "rettet_verdata_florida.txt"
    file_open = open(file_opening, "r")
    writing_file = input("Which file do you want it written to, sir?: ") or "file_for_data.txt"
    year = float(input("And which year do you want it from?: "))
    #Constants and sums
    number_of_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    sum_temp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    sum_wind = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    #For leap years
    if year % 4 == 0:
        number_of_days[2] = 29

    #In case the teacher tries anything funny
    if (writing_file == "rettet_verdata_florida.txt" or "verdata_kirkenes.txt" or "verdata_florida.txt"):
        writing_file = "file_for data.txt"

    #Makes the writing file, and picks the data for that year
    store_data = open(writing_file, "w")
    for line in file_open:
        try:
            split_numbers = line.split('t')
            date_info = split_numbers[1].split('.')
            year_of_line = float(date_info[2])
            month_of_line = float(date_info[1])
            if year_of_line == year:
                sum_temp[int(month_of_line)] += float(split_numbers[8])
                sum_wind[int(month_of_line)] += float(split_numbers[5])
        except Exception:
            pass

    #Prints everything out, plus stores it in the file specified in "writing_file"
    year_in_comma = (float(year))/10000
    print("DatotTAMtFFM")
    for i in range(1, 13):
        print(i + year_in_comma, sum_temp[i] / number_of_days[i] , sum_wind[i] / number_of_days[i], sep = "t", file = store_data) 
        print(i + year_in_comma, sum_temp[i] / number_of_days[i] , sum_wind[i] / number_of_days[i], sep = "t")

    store_data.close()
    file_open.close()

这里的一个问题是:你有:

year = float(input("And which year do you want it from?: "))

year是一个float.然后稍后你有:

date_info = split_numbers[1].split('.')
year_of_line = date_info[2]
month_of_line = date_info[1]
if year_of_line == year: 

其中year_of_linestr,条件总是假的。因此,您的数据始终只是零。如果您一直在使用 python 2.x ,则此问题可能更容易检测,因为您的结果将全部0而不是0.

最新更新