Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'disclosure:': 0.07; 'csv': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:74.54.199': 0.09; 'received:74.54.199.50': 0.09; 'received:gator410.hostgator.com': 0.09; 'to:name:python list': 0.09; '~ethan~': 0.09; 'def': 0.13; 'wrote:': 0.14; 'iterator': 0.16; 'keyerror:': 0.16; 'subject:Pythonic': 0.16; 'top,': 0.16; 'possibly': 0.16; 'bother': 0.19; 'skip:r 30': 0.19; 'header:In- Reply-To:1': 0.22; 'file,': 0.22; 'specified': 0.22; 'skip:i 30': 0.23; 'skip:k 20': 0.23; 'example': 0.24; "didn't": 0.25; 'chris': 0.27; 'subject:?': 0.29; 'are.': 0.29; 'keywords': 0.29; 'usable': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; '...': 0.32; 'break': 0.33; 'header:User-Agent:1': 0.35; 'module.': 0.35; 'try:': 0.35; 'table': 0.37; 'to:addr:python.org': 0.39; 'except': 0.39; 'whatever': 0.39; 'header:Received:5': 0.40; 'might': 0.40; 'received:74.54': 0.60; 'received:hostgator.com': 0.64; 'records': 0.66; 'received:websitewelcome.com': 0.71 Date: Fri, 15 Apr 2011 06:56:55 -0700 From: Ethan Furman User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: python list Subject: Re: Pythonic infinite for loop? References: In-Reply-To: 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 - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-98-246-128-8.hsd1.or.comcast.net ([192.168.74.7]) [98.246.128.8]:3359 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 40 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302875811 news.xs4all.nl 32470 [::ffff:82.94.164.166]:54857 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3271 Chris Angelico wrote: > lst=[] > for i in xrange(1,10000000): # arbitrary top, don't like this > try: > lst.append(parse_kwdlist(dct["Keyword%d"%i])) > except KeyError: > break Possibly overkill: import dbf table = dbf.from_csv("csvfile") # fields get names f0, f1, f2, ... table.rename_field('f3', 'key') # or whatever def keyword_only(record): if record.key.startswith('keyword'): return int(record.key[len('keyword'):])) return dbf.DoNotIndex keywords = table.create_index(keyword_only) # keywords is usable as an iterator for rec in keywords: .... # if you only want the first contiguous records for i, rec in enum(keywords): if rec.key != 'keyword%d' % enum: break ... Disclosure: I am the author of the dbf module. Depending on your needs for the other fields of the csv file, this might be an easier way to access them. Field names can also be specified when opening the csv file, I didn't bother for this example since I don't know what all your field names are. ;) Hope this helps! ~Ethan~