find max and min values from a column of a csv file

prakash jp prakash.stack at gmail.com
Thu Jul 7 05:58:25 EDT 2011


Hi All ,

Could any one help to get max and min values from a specified column of a
csv file. The* csv file is large* and hence the below code did go bad.
*Alternate
methods would be highly appreciated
**
minimum.py*:
import csv
filename = "testLog_4.csv"
f = open(filename)
def col_min(mincname):
    with open(filename, 'rb') as f:
        reader = csv.reader(f, delimiter=",")
        #print reader
        #print mincname
        col_index = next(reader).index(mincname)
        #print col_index
        least = min(rec[col_index] for rec in reader)
    print least

col_min(str("Server Latency"))
col_min(str("Client Latency"))
f.close()

*maximum.py:*
import csv
filename = "testLog_4.csv"
f = open(filename)
def col_max(maxcname):
    with open(filename, 'rb') as f:
        reader = csv.reader(f, delimiter=",")
        #print reader
        #print cname
        col_index = next(reader).index(maxcname)
        #print col_index
        highest = max(rec[col_index] for rec in reader)
    print highest

col_max(str("Server Latency"))
col_max(str("Client Latency"))
f.close()


Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110707/843a7163/attachment.html>


More information about the Python-list mailing list