Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'insert': 0.05; 'reason,': 0.07; 'subject:file': 0.07; 'subject:skip:s 10': 0.07; 'rows,': 0.09; 'cc:addr:python-list': 0.11; '-tkc': 0.16; 'columns': 0.16; 'csv': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'headers,': 0.16; 'subject: \n ': 0.16; 'subject:CSV': 0.16; 'subject:values': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'module': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'headers': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'leave': 0.29; "doesn't": 0.30; 'ease': 0.30; 'moved': 0.30; 'extract': 0.31; 'file': 0.32; 'beginning': 0.33; 'subject:the': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'form.': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'two': 0.37; 'being': 0.38; 'either': 0.39; 'easy': 0.60; 'manually': 0.60; 'email addr:gmail.com': 0.63; 'pick': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'around,': 0.84; 'received:50.22': 0.84; 'same,': 0.91 Date: Mon, 23 Sep 2013 12:47:11 -0500 From: Tim Chase To: quarantinemiles@gmail.com Subject: Re: What's the best way to extract 2 values from a CSV file from each row systematically? In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; 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 X-Get-Message-Sender-Via: boston.accountservergroup.com: none 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379958332 news.xs4all.nl 15966 [2001:888:2000:d::a6]:36552 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54656 On 2013-09-23 10:10, quarantinemiles@gmail.com wrote: > based on two values I want to extract from a CSV file. The > CSV has at least 1000 rows, an example: > > 0,0,KGD,0,DME,0,,0,0 [snip] > I'd like to automatically go through each row in the CSV file from > beginning to end to extract the two values in columns 3 and 5 and > insert them into fields in a form. The csv module has several tools that make this easy to do. If there are column-headers, you can do import csv with file("myfile.csv", "rb") as f: for row in csv.DictReader(f) insert_fields_into_form( row["KGD"], row["DME"], ) which I like for clarity, ease of updating, and robustness (if for some reason, the columns get moved around, or columns get added/removed, as long as the headers remain the same, you can get the data). If it doesn't have headers, then you'd have to manually pick out the columns, either by tuple-unpacking: with file("myfile.csv", "rb") as f: for _, _, kgd, _, dme in csv.reader(f) insert_fields_into_form(kgd, dme) or by directly indexing: KGD_COL = 3 DME_COL = 5 with file("myfile.csv", "rb") as f: for row in csv.reader(f) insert_fields_into_form( row[KGD_COL], row[DME_COL], ) both of which are more fragile than DictReader when it comes to columns being added/removed. I leave the implementation of insert_fields_into_form() up to you :-) -tkc