简体   繁体   English

如何通过使用Python对CSV文件中的日期进行排序来获取列中的最大值?

[英]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? 如何通过使用Python对CSV文件中的日期进行排序来获取列中的最大值?

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. 我能够对最大值进行排序,但无法通过使用Python脚本在CSV文件中对所需日期进行排序来弄清楚如何获得最大值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定使用 python 的最后一列的值,我将如何获得 csv 文件中第一列或第二列的值 - How would I get the value of first or second column in csv file given the value of last column using python 如何使用Python获取字母数字列表中的最高值? - How can I grab the highest value in alphanumeric list using Python? 如何在Python中将csv文件行列的值转换为(行,列,值) - How can I turn csv file row column value into (row, column, value) in Python 如何在csv文件中查找包含某行最高值的列名 - How to find column name that contains the highest value of a row in a csv file 如何使用 Python 和 Z251D1BBFE9A3B678CEAZ30DC 将 csv 文件中一个单元格的值复制到另一个 csv 文件? - How can I copy the value of one cell in a csv file to another csv file using Python and Pandas? 如何在没有熊猫的情况下使用python从CSV文件中获取10个最大值 - How to get the 10 highest values from a CSV file using Python without Pandas 文件 csv:如何使用 python 根据其他列值计算值的出现次数? - file csv: how can I count occourrences of value based on other column value with python? 如何使用 python 中的 pandas 转换 csv 文件中的列中的日期格式? - How do I convert date format in a column in a csv file using pandas in python? 如何将 csv 文件中的元素从最低到最高排列? - how can i rank elements in a csv file from lowest to highest? 如何使用python从csv文件中删除一行,其中缺少任何列的值 - How can I remove a row from csv file with python where a value of any column is missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM