[Tutor] don't understand iteration

Clayton Kirkwood crk at godblessthe.us
Mon Nov 10 01:34:29 CET 2014

I have the following code:


import urllib.request,re,string

months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.',
'Sep.', 'Oct.', 'Nov.', 'Dec.']

from urllib.request import urlopen

for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):

    line = line.decode('utf-8')  # Decoding the binary data to text.

    if 'EST' in line or 'EDT' in line:  # look for Eastern Time

         blah =
re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)


         (month, day, time, ap, offset) = blah.group(1,2,3,4,5)


         print(blah,'\n',ap,month, offset,day, time )
#which gives:


<_sre.SRE_Match object; span=(0, 28), match='<BR>Nov. 09, 07:15:46 PM EST'> 

 PM Nov. EST 09 07


This works fine, but in the (month... line, I have blah.group(1,2,3,4,5),
but this is problematic for me. I shouldn't have to use that 1,2,3,4,5
sequence. I tried to use many alternatives using:  range(5) which doesn't
work, list(range(5)) which actually lists the numbers in a list, and several
others. As I read it, the search puts out a tuple. I was hoping to just
assign the re.search to month, day, time, ap, offset directly. Why wouldn't
that work? Why won't a range(5) work? I couldn't find a way to get the len
of blah.


What am I missing?




You can tell the caliber of a man by his gun--c. kirkwood


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141109/b61cc7d7/attachment.html>

More information about the Tutor mailing list