By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
11 Replies


P: n/a
On Nov 4, 1:20*pm, Gilles Ganault <nos...@nospam.comwrote:
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:
sorted( list( set( domain ) - set( exceptions ) ) )

Set subtraction.
Nov 4 '08 #2

P: n/a
On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
<ca********@gmail.comwrote:
>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 <nos...@nospam.comwrote:
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.
I would just do something like this (not tested):

url_tmpl = "http://www.acme.com/list?code=%02d"
p = re.compile("^(\d+)\t(.+)$")
EXCLUDED_VALUES = 4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89

for i in xrange(1,100):
if i in EXCLUDED_VALUES:
continue
f = urllib.urlopen(url_tmpl % i)
try:
for line in f:
m = p.search(line)
if m:
code = m.group(1)
label = m.group(2)

print "Code=", code, "# Label=", label
finally:
f.close()
Nov 4 '08 #4

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 <nos...@nospam.comwrote:
On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady

<castiro...@gmail.comwrote:
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]

Nov 4 '08 #6

P: n/a
On Tue, 4 Nov 2008 12:10:28 -0800 (PST), Matimus <mc******@gmail.com>
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 <castiro...@gmail.comwrote:
On Nov 4, 1:26*pm, Gilles Ganault <nos...@nospam.comwrote:
On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
<castiro...@gmail.comwrote:
>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 <nos...@nospam.comwrote:
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:
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):
....

Nov 5 '08 #10

P: n/a
On 04 Nov 2008 22:34:49 GMT, Steven D'Aprano
<st***@REMOVE-THIS-cybersource.com.auwrote:
>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 <wu*****@gmail.com>
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.