Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9040

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'passes': 0.05; 'mode,': 0.07; '#print': 0.09; 'comment,': 0.09; 'compute': 0.09; 'csv': 0.09; 'filename': 0.09; 'range,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'binary': 0.13; 'subject:file': 0.13; '"client': 0.16; '"f"': 0.16; '"with"': 0.16; "'rb')": 0.16; 'bieber': 0.16; 'declaimed': 0.16; 'email addr:ix.netcom.com': 0.16; 'email name:wlfraed': 0.16; 'f.close()': 0.16; 'fin.close()': 0.16; 'from:addr:ix.netcom.com': 0.16; 'from:addr:wlfraed': 0.16; 'from:name:dennis lee bieber': 0.16; 'prakash': 0.16; 'received:66.245': 0.16; 'received:dsl.mindspring.com': 0.16; 'received:mindspring.com': 0.16; 'received:wlfraed': 0.16; 'statement)': 0.16; 'subject:csv': 0.16; 'subject:find': 0.16; 'subject:max': 0.16; 'url:netcom': 0.16; 'url:wlfraed': 0.16; 'wulfraed': 0.16; 'def': 0.16; 'appropriate': 0.20; 'column': 0.22; 'code': 0.24; 'url:home': 0.25; 'specified': 0.25; 'function': 0.26; 'pass': 0.28; 'lee': 0.28; 'thu,': 0.28; 'import': 0.29; 'server': 0.29; 'actions.': 0.30; 'nested': 0.30; 'looks': 0.30; 'least': 0.31; 'values': 0.31; 'error': 0.31; 'print': 0.32; 'file.': 0.32; 'it.': 0.33; 'done': 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints- To:1': 0.34; 'record': 0.34; '...': 0.34; 'appreciated': 0.34; 'creates': 0.34; 'charset:us-ascii': 0.36; 'skip:" 10': 0.36; 'file': 0.36; 'fastest': 0.37; 'unless': 0.37; 'but': 0.37; 'could': 0.37; 'using': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'purposes': 0.38; 'two': 0.38; 'should': 0.39; 'header:Mime- Version:1': 0.39; 'data': 0.39; 'client': 0.39; 'subject:from': 0.39; 'help': 0.39; 'to:addr:python.org': 0.39; 'header': 0.40; 'did': 0.40; 'methods': 0.40; 'collect': 0.62; 'closed': 0.62; 'below': 0.62; 'opened': 0.65; 'here': 0.66; 'dennis': 0.77; '"did': 0.84; 'min': 0.84; 'server)': 0.84; 'skip:9 20': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: find max and min values from a column of a csv file
Date Thu, 07 Jul 2011 10:18:57 -0700
Organization > Bestiaria Support Staff <
References <CAJyBqUfFQvwfQ5i2PGy6b2HALhSOd7to-HvbAJMw_o1bDPc4Ag@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host user-11fb7s3.dsl.mindspring.com
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.754.1310059151.1164.python-list@python.org> (permalink)
Lines 126
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1310059151 news.xs4all.nl 21789 [2001:888:2000:d::a6]:51452
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9040

Show key headers only | View raw


On Thu, 7 Jul 2011 15:28:25 +0530, prakash jp <prakash.stack@gmail.com>
declaimed the following in gmane.comp.python.general:

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

	"did go bad", HOW?

	Off-hand, it looks obscenely slow with lots of redundant actions.


> *Alternate
> methods would be highly appreciated
> **
> minimum.py*:
> import csv
> filename = "testLog_4.csv"
> f = open(filename)

	You opened the file as "f"

> def col_min(mincname):
>     with open(filename, 'rb') as f:

	You just REOPENED the file in binary mode, 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)

	You read the entire file to collect just one column of data and
compute a min on it.

	The "with" block exits, and "f" is closed (that's one of the
purposes of the "with" statement)

>     print least
> 
> col_min(str("Server Latency"))
> col_min(str("Client Latency"))

	You've just done TWO passes over the file.

> f.close()

	And closed a file that is already closed... Unless "with" creates a
nested scope so its "f" is a different "f"

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

	And here you repeat the operation using max and two more passes over
the file.

	Side comment, you only need one function to do the process part...
Not two...

def collectData(colName, function):
	...
	return function(rec[col_index] for rec in reader)

col_max = collectData("Server Latency", max)
col_min = collectData("Server Latency", min)

	But fastest should be to do just one pass over the data

UNTESTED
-=-=-=-=-=-=-
import csv

DATAFILE = "testLog_4.csv"

c_min = 9999999999999999999L    #I don't know the domain range, pick a
suitable
s_min = 9999999999999999999L
c_max = -9999999999999999999L
s_max = -9999999999999999999L

fin = open(DATAFILE, "rb")
rdr = csv.reader(fin)

#   NO ERROR TRAPPING

header = rdr.next()
clientIndex = header.index("Client Latency")
serverIndex = header.index("Server Latency")

for record in rdr:
    client = long(record[clientIndex])  #use appropriate data type
    server = long(record[serverIndex])
    c_min = min(c_min, client)
    c_max = max(c_max, client)
    s_min = min(s_min, server)
    s_max = max(s_max, server)

print "Server Latency min/max: %s/%s" % (s_min, s_max)
print "Client Latency min/max: %s/%s" % (c_min, c_max)

fin.close()
del rdr
-=-=-=-=-=-=-
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: find max and min values from a column of a csv file Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-07-07 10:18 -0700

csiph-web