Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168703
Image: ubuntu2004
import urllib, string, time
KP_URL = 'http://sec.noaa.gov/rpc/costello/ace_pkp_15m.txt' a = urllib.urlopen(KP_URL) b = a.read() a.close() localfile = '/tmp/get_kp.txt'; a = open(localfile,'w'); a.write(b); a.close(); a = open(localfile,'r'); lines = [] while 1: line = a.readline() # check for end of file if not line: print "shouldn't have hit this ?? (exit)" exit # ignore lines starting with '#' or ':' --- they're comments if line[0] == ':' or line[0] == '#': continue # parse the line into bits. The last string is a Kp value g = string.split(line); # the string '-1.00' marks "no value available yet" if g[-1] == '-1.00': break else: lines = line, lines[:] a.close(); # done with the file now. # print out the current time (UT) t = time.gmtime(time.time()) print '# current time UT %d %02d %02d %02d%02d' % (t[0],t[1],t[2],t[3],t[4]) # now pring s = string.split(lines[0]); # print out the time at which the last Kp is available print '# recent Kp at UT %s %s %s %s' % (s[0],s[1],s[2],s[3]) kp = s[-1] print kp
Traceback (most recent call last): localfile = '/tmp/get_kp.txt'; File "", line 1, in <module> File "/tmp/tmpgqzwmB/___code___.py", line 5, in <module> a = urllib.urlopen(KP_URL) NameError: name 'urllib' is not defined