Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python if-else语句仅对if条件返回true_Python_List_If Statement_File Handling - Fatal编程技术网

python if-else语句仅对if条件返回true

python if-else语句仅对if条件返回true,python,list,if-statement,file-handling,Python,List,If Statement,File Handling,我正在使用python从文件中读取数值天气数据,然后检查湿度条件。如果湿度小于或等于75,则应将湿度重新写入“低”,如果湿度大于75,则应将其重新写入“高”。以下是我在文件中的数据 外观、温度、湿度、大风、游戏许可 桑尼,85,85,假,不 桑尼,80,90,对,不 阴,83,86,假,对 雨,70,96,假,是的 雨,68,80,假,是的 雷恩,65,70,没错,不是 乌云密布,64,65,没错,是的 桑尼,72,95,错,没有 桑尼,69,70,错,是的 雨,75,80,假,对 桑尼,75,

我正在使用python从文件中读取数值天气数据,然后检查湿度条件。如果湿度小于或等于75,则应将湿度重新写入“低”,如果湿度大于75,则应将其重新写入“高”。以下是我在文件中的数据

外观、温度、湿度、大风、游戏许可

  • 桑尼,85,85,假,不
  • 桑尼,80,90,对,不
  • 阴,83,86,假,对
  • 雨,70,96,假,是的
  • 雨,68,80,假,是的
  • 雷恩,65,70,没错,不是
  • 乌云密布,64,65,没错,是的
  • 桑尼,72,95,错,没有
  • 桑尼,69,70,错,是的
  • 雨,75,80,假,对
  • 桑尼,75,70,没错,是的
  • 乌云密布,72,90,没错
  • 阴天,81,75,假,对
  • 雷恩,71,91,没错,不是
  • 我正在列表中读取此文件,然后访问湿度值。下面是我编写的代码

    def fetchData(fileName):
        datalist = []
        rd =open(fileName,mode='r')
        list = rd.readlines()
        for l in list:
            subList = l.strip().split(',')
            humidity=subList[2]
            if humidity>75:
                subList[2]="high"
            else:
                subList[2]="low"
            datalist.append(subList)
        return datalist
    
    dataList = fetchData('weather.numeric.data')
    print dataList
    
    执行此操作后,数据编号6、7、9、11、13的湿度值应为低,其他应为高。但是所有的湿度值都变得很高,如下面的输出所示

    [['sunny', '85', 'high', 'FALSE', 'no'], ['sunny', '80', 'high', 'TRUE', 'no'], ['overcast', '83', 'high', 'FALSE', 'yes'], ['rainy', '70', 'high', 'FALSE', 'yes'], ['rainy', '68', 'high', 'FALSE', 'yes'], ['rainy', '65', 'high', 'TRUE', 'no'], ['overcast', '64', 'high', 'TRUE', 'yes'], ['sunny', '72', 'high', 'FALSE', 'no'], ['sunny', '69', 'high', 'FALSE', 'yes'], ['rainy', '75', 'high', 'FALSE', 'yes'], ['sunny', '75', 'high', 'TRUE', 'yes'], ['overcast', '72', 'high', 'TRUE', 'yes'], ['overcast', '81', 'high', 'FALSE', 'yes'], ['rainy', '71', 'high', 'TRUE', 'no']]
    
    我应该做什么改变?
    提前感谢!:)

    在与
    75
    进行比较之前,应将字符串转换为
    int

    if int(humidity)>75:
    

    在与
    75
    进行比较之前,应将字符串转换为
    int

    if int(humidity)>75:
    

    1使用“with”语句打开文件:

     with open (filename, "r") as rd:
        #don't use list - it is a type name
        mylist = rd.readlines()
     for l in mylist .....
    
    2将值强制转换为int:

    try humidity = int(humidity):
        if humidity>75:
            subList[2]="high"
        else:
            subList[2]="low"
    except:
        return 'bad values in file - humidity is not a number'
    

    1使用“with”语句打开文件:

     with open (filename, "r") as rd:
        #don't use list - it is a type name
        mylist = rd.readlines()
     for l in mylist .....
    
    2将值强制转换为int:

    try humidity = int(humidity):
        if humidity>75:
            subList[2]="high"
        else:
            subList[2]="low"
    except:
        return 'bad values in file - humidity is not a number'
    

    您正在读取字符串并将其与
    int
    s进行比较!在与
    75
    进行比较之前,将其转换为
    int
    湿度=int(子列表[2])
    您正在读取字符串并将其与
    int
    s进行比较!在与
    75
    进行比较之前,强制转换为
    int
    湿度=int(子列表[2])