[2.5.1] Building loop with some exceptions?

G

Gilles Ganault

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.
 
A

Aaron Brady

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.
 
M

Matimus

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=d"
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()
 
B

bearophileHUGS

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
 
G

Gilles Ganault

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.
 
S

Steven D'Aprano

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
 
A

alex23

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):
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top