Okay, thanks!

    listdata4 = data4.tolist()
    strdata4 = str(listdata4)
    strdata4 = strdata4.replace('[','').replace(']','')

and then

    outfile2=open(outfilename2,'w')
    for element in strdata4:
        outfile2.write(element)

The comma separation and format is okay for this application.

The enumerate function looks handy too, thank you again.

Cheers,
Heather











Quoting Jeff Schwaber <[email protected]>:

On Thu, Mar 11, 2010 at 12:12 PM, <[email protected]> wrote:

I commented out the lines you suggested and here is the new error:new last
part of code:

##########################
#RENAME DATA FOR WRITING
#########################
   os.chdir(resampleDIR)
   h=filex.replace( '.', '_' )
   outfilename=h+'.txt'
   outfilename2=h+'NA.txt'
   outfile2=open(outfilename2,'w')
   data4.tolist(outfile2)



###################################
#WRITE NEW DATA WITH HEADER ON TOP
###################################

   os.chdir(resampleDIR)

#try:
   with open(outfilename,'w') as outfile:
       outfile.write(header)
       with open(outfilename2,'r') as datafile:
           for line in datafile:
               outfile.write(line)

Error message for that:


Traceback (most recent call last):
 File "C:\PDSIwork\PYTHON_SCRIPTS\RESAMPLER2.py", line 65, in <module>
   data4.tolist(outfile2)
TypeError: tolist() takes exactly 0 arguments (1 given)


Heather,

tolist seems to convert the array to a list. So:

listdata4 = data4.tolist()

I would then expect listdata4 to be a list. Looking at the files, you're
trying to take a list

[1,2,3,4]

and get it into the file as

1,2,3,4

So the way you tried at the beginning could work now, if you convert the
list to a string first:

listdata4 = data4.tolist()
strdata4 = str(listdata4)
strdata4 = strdata4.replace('[', '').replace(']','')

But it might not be obvious to programmers not expecting that method.

You could do a for loop:

for element in listdata4:
    outfile2.write(element)

But putting in just the right number of commas is irritating. Still might be
the best way:

for i,element in enumerate(listdata4):
    if i:
        outfile2.write(', ')
    outfile2.write(element)

To explain that code:

for i,element in enumerate([1,2,3,4]):
    print i,e

prints out

0 1
1 2
2 3
3 4

And 0 is false, 1-3 are true.

I think there were other solutions in other emails, I just know it's hard to
figure out what's going on at the beginning.

Jeff


Also, I attached the type of file I get (using slightly altered code) if I
don't use NumArray, and I just read in a plain list, it is not the correct
format for GIS to accept.

Thank you,
Heather





_______________________________________________
Portland mailing list
[email protected]
http://mail.python.org/mailman/listinfo/portland

Reply via email to