簡體   English   中英

如何通過使用Python對CSV文件中的日期進行排序來獲取列中的最大值?

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

如何通過使用Python對CSV文件中的日期進行排序來獲取列中的最大值?

我的代碼:

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" 

我能夠對最大值進行排序,但無法通過使用Python腳本在CSV文件中對所需日期進行排序來弄清楚如何獲得最大值。

使用此片段

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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM