alright, so i'm trying to import text from a file into an array so that I have the ability to edit data, find certain functions, etc.
This is currently what my program looks like....(I haven't gotten very far)

from scipy import *
from numpy import *

def Radiograph_data:
try:
file('c:/users/ross/desktop/radiograph_data.txt') #checks desktop for file
except IOerror, e:
e = urlopen('http://www.u.arizona.edu/~erdmann/mse350/radiograph_data.txt') #downloads file from internet


data = numpy.loadtxt(e, dtype = float32, comments = '!') #places text in array without lines starting with '!'


Thanks,

Ross

Go, Wildcats!

When posting code you should wrap it around code=Python and /code tags, thusly:

from scipy import *
from numpy import *

def Radiograph_data:
  try:
    file('c:/users/ross/desktop/radiograph_data.txt') #checks desktop for file
  except IOerror, e:
    e = urlopen('http://www.u.arizona.edu/~erdmann/mse350/radiograph_data.txt') #downloads file from internet
  data = numpy.loadtxt(e, dtype = float32, comments = '!') #places text in array without lines starting with '!'

The numpy.loadtxt() call is not right because of your from..import form, but I suspect that's an artifact of repeated tries. Just loadtxt(), but that doesn't work either.

numpy looks at the handle you give it (e, above) and checks if it has a seek attribute, which it doesn't, and fails, concluding e is not file-like. This is probably a bug in numpy, or at least an oversight since enumerate(e) works and that's what numpy needs. But it wrongly concludes otherwise.

The most expedient thing to do may be to just write the URL data to a local file and pass that handle to loadtxt(), viz:

e = urlopen('http://www.u.arizona.edu/~erdmann/mse350/radiograph_data.txt')
tmpfile = "x.tmp"
f = open(tmpfile,"w")
f.write(e.read())
e.close()
f.close()
e = open(tmpfile,"r")
a = loadtxt(e, float32, comments = '!')
print a

array([[ 1.75999999e-02, 7.06145996e+03],
[ 2.21999995e-02, 1.00292803e+04],
[ 2.98999995e-02, 1.51805303e+04],
[ 3.68000008e-02, 2.00456602e+04],
[ 4.58000004e-02, 2.62881406e+04],
[ 5.53999990e-02, 3.31531914e+04],
[ 6.40000030e-02, 4.00034414e+04],
[ 7.29999989e-02, 4.74811992e+04],
[ 8.20000023e-02, 5.52806016e+04]], dtype=float32)

commented: Very nice answer +6

If you want to use backslashes in a path name for loadtxt (or savetxt), the you need to put in double backsplashes so:
'c:/users/ross/desktop/radiograph_data.txt'
becomes:
'c://users//ross//desktop//radiograph_data.txt'

If you want to use backslashes in a path name for loadtxt (or savetxt), the you need to put in double backsplashes so:
'c:/users/ross/desktop/radiograph_data.txt'
becomes:
'c://users//ross//desktop//radiograph_data.txt'

Those are forward slashes, which do not need to be escaped (the escape character is \, which is backslash)

The most expedient thing to do may be to just write the URL data to a local file and pass that handle to loadtxt()...

If the amount of data is not huge, it's simpler to use StringIO as a temporary in-memory file:

from cStringIO import StringIO
e = urlopen('http://www.u.arizona.edu/~erdmann/mse350/radiograph_data.txt')
try: tmp = StringIO(e.read())
finally: e.close()
a = loadtxt(tmp, float32, comments = '!')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.