[portland] Array writing question from landscape ecologist
Jeff Schwaber
freyley at gmail.com
Thu Mar 11 20:26:02 CET 2010
On Thu, Mar 11, 2010 at 8:53 AM, <lintzh at science.oregonstate.edu> wrote:
> CODE STARTS HERE:
>
> from __future__ import with_statement
>
> import arcgisscripting, sys, os, string, copy, glob,numarray
>
> # Create the Geoprocessor Object
> gp = arcgisscripting.create()
>
>
> ##############
> #DIRECTORIES
> ##############
>
> workDIR = "C:\\PDSIwork"
> resampleDIR = workDIR + "\\resample\\"
> ascDIR = workDIR + "\\PDSIdata\\"
>
> ##############
> #HEADER
> ##############
>
> header = """\
> ncols 1386
> nrows 595
> xllcorner -124.7292
> yllcorner 24.6024
> cellsize 0.0417
> NODATA_value -9999
> """
>
> #############################
> #GET RUNLIST AND IMPORT DATA
> ##############################
>
> data=[]
> os.chdir(ascDIR)
> runlist=os.listdir(ascDIR)
> #print runlist
> for filex in runlist:
> x=open(filex,'r')
> for i in xrange(824670):
> z=x.readline()
> z=float(z)
> data.append(z)
> print filex
>
> #############
> #RESHAPE DATA
> #############
>
> data2 = numarray.array(data)
> data3 = numarray.reshape(data2, 595,1386)
> data4=numarray.transpose(data3)
> print len(data4)
>
> ##########################
> #RENAME DATA FOR WRITING
> #########################
>
> os.chdir(resampleDIR)
> h=filex.replace( '.', '_' )
> outfilename=h+'.txt'
>
> ###################################
> #WRITE NEW DATA WITH HEADER ON TOP
> ###################################
>
>
> #try:
> with open(outfilename,'w') as outfile:
> outfile.write(header)
> for line in data4:
> line.replace('[',' ').replace(']',' ')
> outfile.write(line)
>
>
>
> ---------------------------
> Here's the error message:
> ---------------------------
>
> Traceback (most recent call last): File
> "C:/PDSIwork/PYTHON_SCRIPTS/RESAMPLER2b.py", line 74, in <module>
> line.replace('[',' ').replace(']', ' ')
> AttributeError: 'NumArray' object has no attribute 'replace'
>
>
I'm also not sure how numarrays work, and not sure why you're using them
instead of regular lists. What I see, however, is that this code is
erroring very near the end on the 'line.replace' method call, and that looks
like an attempt to format the data to print it to the file.
I think what you're trying to do is you have this array of numbers and you
want to print it out as [a,b,c,d] in the file. Is that what line 74 is
trying to do?
What happens if you comment out line 74 (the line.replace... line)? It
should write out a file that doesn't quite look like what you want, but if
you were to post a line or two of that, and a line or two of what you want,
it might really help clarify what needs to change.
Thanks,
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/portland/attachments/20100311/470c2a66/attachment.html>
More information about the Portland
mailing list