Kent and Danny,

Thanks for the help.  The trick was to put the .read() at the end of the
urllib2 line.  It makes some sense now but man I hate it when it is that
simple and I just can't see it.

Thanks again for the great help.

John Ertl 

-----Original Message-----
From: Kent Johnson [mailto:[EMAIL PROTECTED]
Sent: Tuesday, April 19, 2005 10:33
Cc: tutor@python.org
Subject: Re: FW: [Tutor] Trying to d0 HTTP GET

Ertl, John wrote:
> All,
>
> I have figured out a bit more.  I can get the binary values from the
service
> but I think they come back as a single string.  How do I read that into an
> array?  The code below will read the first number into the array and print
> it out but how would I read the whole thing into an array...I would like
to
> skip the step of putting the raw binary numbers into a variable and
instead
> read it directly into the binvalues array.
>
> I have tried things like  binvalues.read(rawData.read(4,size of array))
and
> a few other things but none of them work.  I was hoping for a fromstream
but
> no luck no that either.

Unless the data is huge you should probably just read it all into a string,
then pass the string to
binvalue like this:

binvalues = array.array('f')
rawData = urllib2.urlopen(...).read()

binvalues.fromstring(rawData)
binvalues.byteswap()

This is likely to be the fastest approach as all the looping happens
internally to urllib2 and
array. The only limitation is that both representations have to fit in
memory at once.

Alternately you could wrap the rawData in a generator function which returns
floats. Then pass the
generator to binvalues.extend(). Something like this (untested):

import array, struct, urllib2

def generateFloats(rawDataStream):
   while True:
     s = rawData.read(4)
     if len(s) < 4: return
     f = struct.unpack('f', s) # prefix the 'f' with the correct byte-order
character...
     yield f

binvalues = array.array('f')
rawDataStream = urllib2.urlopen(...)

binvalues.extend(generateFloats(rawDataStream))

Kent


> Thanks for any help.
> <CODE>
> binvalues = array.array('f')
>
> rawData =
>
urllib2.urlopen("http://dsd1u:7003/GRID:U:NOGAPS:2005041800:global_360x181:a
> ir_temp:ht_sfc:00020000:00000000:fcst_ops:0240")
>
>
> binvalues.fromstring(rawData.read(4))  # 4 byte float
>
> binvalues.byteswap()
>
> print binvalues
>
> </CODE>

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to