Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; '(python': 0.07; 'explicit': 0.07; 'indexing': 0.07; 'variables': 0.07; 'cleanup': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '-tkc': 0.16; 'bound.': 0.16; 'csv': 0.16; 'dict(': 0.16; 'enough.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lambda': 0.16; 'notably': 0.16; 'robust.': 0.16; 'situation.': 0.16; 'subject:There': 0.16; 'index': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; '(in': 0.22; 'handles': 0.22; 'cc:addr:python.org': 0.22; 'headers': 0.24; 'simpler': 0.24; 'williams': 0.24; 'header': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'usually': 0.31; 'division': 0.31; 'overhead': 0.31; 'probably': 0.32; 'reader': 0.33; 'skip:d 20': 0.34; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'doing': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'two': 0.37; 'needed': 0.38; 'files': 0.38; 'rather': 0.38; 'does': 0.39; 'major': 0.40; 'how': 0.40; 'times': 0.62; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'believe': 0.68; 'skip:r 30': 0.69; 'hand': 0.80; 'faster.': 0.84; 'indirect': 0.84; 'received:50.22': 0.84; 'notorious': 0.91; 'you).': 0.95 Date: Tue, 23 Apr 2013 09:30:05 -0500 From: Tim Chase To: Neil Cerutti Subject: Re: There must be a better way In-Reply-To: References: <51732d81$0$29977$c3e8da3$5496439d@news.astraweb.com> <20130420193422.25255e98@bigbox.christie.dr> X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366727321 news.xs4all.nl 2169 [2001:888:2000:d::a6]:36443 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44181 On 2013-04-23 13:36, Neil Cerutti wrote: > On 2013-04-22, Colin J. Williams wrote: > > Since I'm only interested in one or two columns, the simpler > > approach is probably better. > > Here's a sketch of how one of my projects handles that situation. > I think the index variables are invaluable documentation, and > make it a bit more robust. (Python 3, so not every bit is > relevant to you). > > with open("today.csv", encoding='UTF-8', newline='') as today_file: > reader = csv.reader(today_file) > header = next(reader) > majr_index = header.index('MAJR') > div_index = header.index('DIV') > for rec in reader: > major = rec[majr_index] > rec[div_index] = DIVISION_TABLE[major] > > But a csv.DictReader might still be more efficient. I never > tested. This is the only place I've used this "optimization". > It's fast enough. ;) I believe the csv module does all the work at c-level, rather than as pure Python, so it should be notably faster. The only times I've had to do things by hand like that are when there are header peculiarities that I can't control, such as mismatched case or added/remove punctuation (client files are notorious for this). So I often end up doing something like def normalize(header): return header.strip().upper() # other cleanup as needed reader = csv.reader(f) headers = next(reader) header_map = dict( (normalize(header), i) for i, header in enumerate(headers) ) item = lambda col: row[header_map[col]].strip() for row in reader: major = item("MAJR").upper() division = item("DIV") # ... The function calling might add overhead (in which case one could just use explicit indirect indexing for each value assignment: major = row[header_map["MAJR"]].strip().upper() but I usually find that processing CSV files leaves me I/O bound rather than CPU bound. -tkc