【问题标题】:lowest and highest from text file - python 3文本文件中的最低和最高 - python 3
【发布时间】:2018-04-23 05:26:28
【问题描述】:

我是 Python 新手,我的代码有问题 我在输出中打印了错误的数据,如日期、年份、最低和最高温度。我不确定需要对代码进行哪些更改才能从文本文件中打印正确的最低和最高温度。谢谢您的帮助。

下面的代码输出如下所示:

日期:02/01
年份:2002
最低温度是:8
日期:02/06
年份:2008
最高温度:77

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))

main()

这是我的文件外观的示例。 (我只从文本文件中包含了我需要的列)

Day     Max   Year   Min    
---------------------------------------
---------------------------------------
02/01   79   2002    8  
02/02   69   1989    5  
02/03   83   1989    7  
02/04   76   1957    3  
02/05   98   1890    0  
02/06   77   2008    9  

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    给你。

    file = open('text.txt', 'r')
    
    lowest = 200
    lowest_day = 0
    lowest_year = 0
    highest = 0
    highest_day = 0
    highest_year = 0
    
    
    for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = int(values[3])
            high = int(values[1])
    
            if low < lowest:
                lowest = low
                lowest_day = values[0]
                lowest_year = values[2]
    
            if high > highest:
                highest = high
                highest_day = values[0]
                highest_year = values[2]
    
    print ("Day: ", (lowest_day))
    print ("Year: ", (lowest_year))
    print ("The lowest temperature is: ", (lowest))
    
    
    print ("Day: ", (highest_day))
    print ("Year: ", (highest_year))
    print ("The highest temperature is: ", (highest))
    

    这给出了输出:

    Day:  02/05
    Year:  1890
    The lowest temperature is:  0
    Day:  02/05
    Year:  1890
    The highest temperature is:  98
    

    一些注释:for lowest in low: 没有意义,因为low 只是一个字符,而不是一个列表或其他东西,所以我删除了它。另外,您正在将整数与字符串进行比较,因此我将字符串转换为整数。

    【讨论】:

    • 当然,如果它对你有用,请标记为正确:)
    【解决方案2】:

    您的错误是因为您有两个名为最低的变量(和两个称为最高)。创建循环时

    for lowest in low:
    

    Python 遍历“low”中的字符并比较它们是否小于“low”(这没有意义)。变量“lowest”已经被这个for循环覆盖了,所以程序给出了错误的结果。另外,为了找到最低温度,与最低值比较的数据必须是数字数据类型,而不是字符串类型。

    要解决此问题,您应该取消“最低线最低”,只需为每天的最低温度创建一个变量(例如 lineLow)。这个变量将等于

    int(values[3])
    

    同样适用于较高的温度。

    (另外,你可以使用file.readlines()代替file.read().splitlines())

    【讨论】:

      【解决方案3】:

      如果您想要一种更 Python 的方式,那么我认为以下代码也可能会有所帮助。

      file = open ('temp.txt', 'r')
      
      myfunc1= lambda s: s[3]
      myfunc2= lambda s: s[1]
      
      lines = [line.strip().split() for line in file.read().splitlines() if line[0].isdigit()]
      
      lowest= sorted(lines, key=myfunc1)[0]
      highest = sorted(lines, key=myfunc2, reverse=True)[0]
      
      print("lowestDAY: {0}, lowestYear: {1}, lowestTemp: {2}".format(lowest[0], lowest[2], lowest[3]))
      print("highestDAY: {0}, highestYear: {1}, highestTemp: {2}".format(highest[0], highest[2], highest[1]))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 2017-02-08
        • 1970-01-01
        • 2019-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多