繁体   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