Finding Maximum Value in CSV File

后端 未结 5 2012
醉梦人生
醉梦人生 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条回答
  •  梦毁少年i
    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
    

提交回复
热议问题