Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16043
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'finally:': 0.09; 'def': 0.13; '-tkc': 0.16; 'f.close()': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lambda': 0.16; 'message-id:@tim.thechases.com': 0.16; 'messy': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'row': 0.16; 'subject:CSV': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'possibly': 0.19; 'cc:no real name:2**0': 0.20; 'header:In-Reply-To:1': 0.22; '(though': 0.23; 'cc:2**0': 0.24; 'cc:addr:python.org': 0.29; '3.x': 0.30; 'capture': 0.32; 'headers': 0.32; 'header:User-Agent:1': 0.33; 'there': 0.33; 'loop': 0.34; 'things': 0.34; 'keys': 0.34; 'try:': 0.34; 'subject:How': 0.35; '...': 0.36; 'should': 0.39; 'might': 0.40; 'love': 0.62; 'subject:Reader': 0.84; 'skip:n 40': 0.85 |
| Date | Mon, 21 Nov 2011 12:36:14 -0600 |
| From | Tim Chase <python.list@tim.thechases.com> |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.20) Gecko/20110826 Icedove/3.1.12 |
| MIME-Version | 1.0 |
| To | ray <ray@aarden.us> |
| Subject | Re: How to: Coordinate DictReader and Reader for CSV |
| References | <2012e1cb-5913-4f39-b102-038a1c95a483@gi1g2000vbb.googlegroups.com> <9iv3q7F1t5U1@mid.individual.net> <04bc73a1-1408-4626-991f-fad94933cb5f@p2g2000vbj.googlegroups.com> |
| In-Reply-To | <04bc73a1-1408-4626-991f-fad94933cb5f@p2g2000vbj.googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| 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-Source | |
| X-Source-Args | |
| X-Source-Dir | |
| Cc | python-list@python.org |
| 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.2928.1321902366.27778.python-list@python.org> (permalink) |
| Lines | 35 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1321902366 news.xs4all.nl 6928 [2001:888:2000:d::a6]:49448 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:16043 |
Show key headers only | View raw
On 11/21/11 09:16, ray wrote:
> Is there a way to capture the keys outside of the for loop so
> when the for loop is entered, only data is extracted?
I frequently do this for things like tweaking headers (stripping
space, normalizing case, etc because clients love to send us
messy data):
def norm_header(h):
return h.strip().upper()
def norm_item(i):
return i.strip()
f = file("example.csv", "rb")
try:
r = csv.reader(f)
headers = r.next()
header_map = dict(
(norm_header(h), i)
for i, h in enumerate(headers)
)
for row in r:
item = lambda h: norm_item(row[header_map[norm_header(h)]])
value1 = item("Item1")
value2 = item("ITEM3")
...
finally:
f.close()
Should work in 2.x, possibly in 3.x (though you might need to
change from "headers = r.next()" to "headers = next(r)")
-tkc
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
How to: Coordinate DictReader and Reader for CSV ray <ray@aarden.us> - 2011-11-21 05:18 -0800
Re: How to: Coordinate DictReader and Reader for CSV Neil Cerutti <neilc@norwich.edu> - 2011-11-21 13:59 +0000
Re: How to: Coordinate DictReader and Reader for CSV ray <ray@aarden.us> - 2011-11-21 07:16 -0800
Re: How to: Coordinate DictReader and Reader for CSV Neil Cerutti <neilc@norwich.edu> - 2011-11-21 15:41 +0000
Re: How to: Coordinate DictReader and Reader for CSV Neil Cerutti <neilc@norwich.edu> - 2011-11-21 15:43 +0000
Re: How to: Coordinate DictReader and Reader for CSV Tim Chase <python.list@tim.thechases.com> - 2011-11-21 12:36 -0600
csiph-web