简体   繁体   中英

How can I get the highest value in a column by sorting the date in a CSV file using Python?

How can I get the highest value in a column by sorting the date in a CSV file using Python?

My code:

if sys.argv[1] == "-p":
    c = sys.argv[2]
    col_name= r"\\DT001\Process(SK)\Virtual"
    #d = sys.argv[3]
    col_name2 = r"(PDH-CSV 4.0) (GMT Standard Time)(0)"

    with open(c, 'rb') as inf:
        reader = csv.reader(inf)
        col_index = next(reader).index(col_name)
        highest = float(max(rec[col_index] for rec in reader))

        reader2 = csv.reader2(inf)
        col_index2 = next(reader).index(col_name2)
        date = '7/19/2011' 


        #gigabytes = highest / 1073741824
        gigabytes = highest /1024/1024/1024
        print "Performance Counters Peak:\n", gigabytes, "GB" 

I was able to sort the highest value but I am unable to figure out how to get the highest value by sorting the desired date in the CSV file using the Python script.

use this snippet

from  time import strptime
def filterByDate(mydate , start,end):
    format = "%m/%d/%Y"
    return strptime(start,format) <= strptime(mydate,format) and strptime(mydate,format) <= strptime(end,format)
...
...
highest = float(max(rec[col_index] for rec in reader if filterByDate(rec[date_col_index],startDate,endDate)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM