Python代码:“ Min”的格式正确,但“ Max”

问题描述 投票:0回答:1

我正在尝试使用天气数据处理csv文件。

文件的组织方式是,第一列是年份,第二列是月份,第三列是月份,第四列是小时(每天00到23),第五列是一年中每一天的每小时露点温度。某些日子每天都有每小时的温度值,有些日子则是-999或缺少数小时。

我正在尝试从此每小时数据中提取最小和最大每日值。为此,我有一个“ if”语句,按月排序,以标识每月的天数。然后在if语句中,我按日期排序以从每小时数据中提取最小露点(minDP)和最大露点(maxDP)。要找到minDP和maxDP,我有单独的min和max函数,它们具有相同的参数参数:对于每个具有小时数据的日期,如果露点大于-999,则返回最小值或最大值,否则返回“ M”缺少”(如果当天的所有小时数据均为-999)。 minDP部分工作正常,但是,maxDP在所有日期都始终返回丢失,我不明白为什么。在maxDP部分中,如果将“ max”功能更改为“ min”,则它会成功返回min。

for m in months:
    if m == '01' or m =='03' or m == '05' or m == '07' or m == '08' or m == '10' or m == '12':
         dates = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
         for d in dates:

             # For each date extract the minimum dewpoint, print "M" if all values for the date are less than -999
             with open('samplehourlydata.txt', 'r') as f:
                reader = csv.reader(f)
                next(reader)     # Skip header
                MinDP = min(row[4] if row[1] == m and row[2] == d and int(row[4]) > -999 else 'M' for row in reader)

            # For each date extract the maximum dewpoint, print "M" if all values for the date are less than -999
             with open('samplehourlydata.txt', 'r') as f:
                reader = csv.reader(f)
                next(reader)  # Skip header
                MaxDP = max(row[4] if row[1] == m and row[2] == d and int(row[4]) > -999 else 'M' for row in reader)
                print(year, m, d, MinDP, MaxDP)

max参数正在执行if语句的“ else”部分的事实使我认为它正在执行我要告诉它的操作。但是,我认为我绝不能告诉它做我想做的事情。但是,只需在maxDP部分中将“ max”更改为“ min”即可返回实际的最小值,这一事实似乎重申了该参数是有效的...

如果有帮助,仅第一天的csv数据如下所示:

yyyy,mm,dd,hour,D.P.
2011,01,01,00, 27
2011,01,01,01, 28
2011,01,01,02, 27
2011,01,01,03, 26
2011,01,01,04, 26
2011,01,01,05, -999
2011,01,01,06, 28
2011,01,01,07, 27
2011,01,01,08, 28
2011,01,01,09, 29
2011,01,01,10, 31
2011,01,01,11, 34
2011,01,01,12, 37
2011,01,01,13, 39
2011,01,01,14, 40
2011,01,01,15, 42
2011,01,01,16, 44
2011,01,01,17, 43
2011,01,01,18, 43
2011,01,01,19, 43
2011,01,01,20, 43
2011,01,01,21, 42
2011,01,01,22, 44
2011,01,01,23, 44

并且错误的maxDP的当前输出看起来像这样:

yyyy mm dd MinDP MaxDP
2011 01 01  26 M

感谢您的任何输入。

python python-3.x max min weather
1个回答
0
投票

尝试

MaxDP = max(row[4] if row[1] == m and row[2] == d and int(row[4]) > -999 else '\0' for row in reader)

'\ 0'是最低字符

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.