455,587 Members | 1,632 Online
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

# [2.5.1] Building loop with some exceptions?

 P: n/a Hello I need to call a URL through a loop that starts at 01 and ends at 99, but some of the steps must be ignored: ===== url = "http://www.acme.com/list?code=" p = re.compile("^(\d+)\t(.+)\$") for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89: f = urllib.urlopen(url + i) page = f.read() f.close() for line in page: m = p.search(line) if m: code = m.group(1) label = m.group(2) print "Code=" + code + " # Label=" + label ===== I'm clueless at what the Python way is to do this :-/ Thank you for any help. Nov 4 '08 #1
11 Replies

 P: n/a On Nov 4, 1:20*pm, Gilles Ganault

 P: n/a On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89: sorted( list( set( domain ) - set( exceptions ) ) )Set subtraction. Thanks a lot but... I don't know what the above means :-/ Nov 4 '08 #3

 P: n/a On Nov 4, 11:20*am, Gilles Ganault

 P: n/a Gilles Ganault: Thanks a lot but... I don't know what the above means :-/ set(iterable) just builds a set, then you use the really usual set semantics. Anyway, maybe you may find this more easy to understand: refused_indexes = set([4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89]) for i in xrange(1, 100): if i not in refused_indexes: print i Note: never put a zero before an integer number (because it will give you troubles, defining octal numbers). Bye, bearophile Nov 4 '08 #5

 P: n/a On Nov 4, 1:26*pm, Gilles Ganault >sorted( list( set( range( 10 ) ) - set( [ 3, 5, 9 ] ) ) ) [0, 1, 2, 4, 6, 7, 8] Nov 4 '08 #6

 P: n/a On Tue, 4 Nov 2008 12:10:28 -0800 (PST), Matimus wrote: >I would just do something like this (not tested): Thanks a lot guys :-) Worked first time. I just have the usual issue with ASCII/Unicode: ======= cursor.execute(sql) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 53: ordinal not in range(128) ======= Is there a way to get rid of this error once and for all? Thank you. Nov 4 '08 #7

 P: n/a On Tue, 04 Nov 2008 20:20:20 +0100, Gilles Ganault wrote: Hello I need to call a URL through a loop that starts at 01 and ends at 99, but some of the steps must be ignored: .... I'm clueless at what the Python way is to do this :-/ Perhaps you should start by doing the Python tutorial, so you'll be less clueless of simple things like how to do a loop. http://docs.python.org/tutorial/ for i in range(1, 100): if i in (4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89): continue do_rest_of_processing -- Steven Nov 4 '08 #8

 P: n/a On Nov 4, 10:02*pm, Aaron Brady for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89: >sorted( list( set( domain ) - set( exceptions ) ) ) >Set subtraction. Thanks a lot but... I don't know what the above means :-/ This example produces the numbers 0..9, except 3, 5, and 9. >sorted( list( set( range( 10 ) ) - set( [ 3, 5, 9 ] ) ) ) [0, 1, 2, 4, 6, 7, 8] FYI, 'set' accepts an iterable, so you don't need 'list': >>sorted(set(range(10)) - set([3, 5, 9])) [0, 1, 2, 4, 6, 7, 8] Nov 4 '08 #9

 P: n/a On Nov 5, 5:20*am, Gilles Ganault

 P: n/a On 04 Nov 2008 22:34:49 GMT, Steven D'Aprano for i in range(1, 100): if i in (4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89): continue do_rest_of_processing Thanks for the sample. Nov 7 '08 #11

 P: n/a On Tue, 4 Nov 2008 15:55:06 -0800 (PST), alex23 wrote: exclusions = [04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89] for i in (x for x in xrange(1,100) if x not in exclusions): Thanks guys. Nov 7 '08 #12

### This discussion thread is closed

Replies have been disabled for this discussion.