Pythonic infinite for loop?
Ethan Furman
ethan at stoneleaf.us
Fri Apr 15 09:56:55 EDT 2011
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~
More information about the Python-list
mailing list