Finding Maximum Value in CSV File

后端 未结 5 2010
醉梦人生
醉梦人生 2021-01-29 06:14

Have an assignment of finding average and maximum rainfall in file \"BoulderWeatherData.csv\". Have found the average using this code:

    rain = open(\"BoulderW         


        
相关标签:
5条回答
  • 2021-01-29 06:54

    This is my prefered way of handling this.

    #!/usr/bin/env python3
    
    rain = open("BoulderWeatherData.csv","r")
    
    average = 0.0
    total = 0
    maxt = 0.0
    
    for line in rain:
        try:
            p = float(line.split(",")[4])
            average += p
            total += 1
            maxt = max(maxt,p)
        except:
            pass
    
    average = average / float(total)
    
    print("Average:",average)
    print("Maximum:",maxt)
    

    This will output:

    Average: 0.05465272591486193
    Maximum: 1.98
    
    0 讨论(0)
  • 2021-01-29 07:06
    import csv
    
    INPUT  = "BoulderWeatherData.csv"
    PRECIP = 4   # 5th column
    
    with open(INPUT, "rU") as inf:
        incsv  = csv.reader(inf)
        header = next(incsv, None)    # skip header row
        precip = [float(row[PRECIP]) for row in incsv]
    
    avg_precip = sum(precip, 0.) / (1 and len(precip))  # prevent div-by-0
    max_precip = max(precip)
    
    print(
        "Avg precip: {:0.3f} in/day, max precip: {:0.3f} in/day"
        .format(avg_precip, max_precip)
    )
    

    returns

    Avg precip: 0.055 in/day, max precip: 1.980 in/day
    
    0 讨论(0)
  • 2021-01-29 07:08
    max=0 
    for line in data:
          r = line.split(",")
          if float(r[4]) > max:
               max=float(r[4])
    print(max)
    

    something like that

    0 讨论(0)
  • 2021-01-29 07:08

    This code will attempt to find the maximum value, and the average value, of floats stored in the 5th position in a .csv.

    rainval = []
    

    Initializes the empty array where we will store values.

    with open ("BoulderWeatherData.csv", "r") as rain:
    

    Opens the .csv file and names it "rain".

        for lines in rain:
    

    This reads every line in rain until the end of the file.

            rainval += [float(lines.strip().split(",")[4])]
    

    We append the float value found in the fifth position (fourth index) of the line.

    We repeat the above for every line located in the .csv file.

    print (sorted(rainval)[len(rainval)])
    

    This sorts the values in the rainval array and then takes the last (greatest) value, and prints it. This is the maximum value and is better than max because it can handle floats and not just ints.

    print (sum(rainval)/len(rainval))
    

    This prints the average rainfall.


    Alternatively, if we don't want to use arrays:

    maxrain = -float("inf")
    total, count = 0, 0
    with open ("test.txt", "r") as rain:
        for lines in rain:
            temp = float(lines.strip().split(",")[4])
            if maxrain < temp:
                maxrain = temp
            total += temp
            count += 1
    
    print (maxrain)
    print (total/count)
    
    0 讨论(0)
  • 2021-01-29 07:19

    You're already accumulating total across loop iterations.

    To keep track of a maxvalue, it's basically the same thing, except instead of adding you're maxing:

    total = 0
    maxvalue = 0
    
    for line in data:
        r = line.split(",")
        value = float(r[4])
        total = total + value
        maxvalue = max(maxvalue, value)
    
    print(total)
    print(maxvalue)
    

    Or, if you don't want to use the max function:

    for line in data:
        r = line.split(",")
        value = float(r[4])
        total = total + value
        if value > maxvalue:
            maxvalue = value
    
    0 讨论(0)
提交回复
热议问题