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

John [H2O] washakie at gmail.com
Thu Jul 7 06:38:04 EDT 2011


I think you would benefit from reading the data into a numpy array first,
then using numpy min, max functions.



prakash jp wrote:
> 
> 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
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: http://old.nabble.com/find-max-and-min-values-from-a-column-of-a-csv-file-tp32011861p32012126.html
Sent from the Python - python-list mailing list archive at Nabble.com.




More information about the Python-list mailing list