473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

[2.5.1] Building loop with some exceptions?

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 1275
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: vineoff | last post by:
If I'm having nested loops like: for (...) for (..) for (...) { /* exit here */ } and I need to exit from there ^ . Is it better to use exceptions or goto or some other method?
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
7
by: Pietje de kort | last post by:
Hello all, I am trying to build a class that does something, and may timeout while doing so. Ofcouse I want to be a bit elegant, so I came up with the code found below. Problem is, I can't catch...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
38
by: yomgui | last post by:
hello, is it legal to return inside a for loop or am I obliged to break out of the loop before returning ? thanks yomgui
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
16
by: Andy B | last post by:
I have the following code inside of a WebBrowser.DocumentCompleted event: For index As Integer = 0 To Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Count ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.