exporting a raster attribute table to a csv or dbf

726
1
04-30-2012 02:09 PM
IanLeinwand
New Contributor II
I need to batch export information from 400 raster attribute tables...

I can get it to write a text file delimited by , but it write all of the records on the same line... how do I get it to recognize the fields as column and
format the text file so I can import it into access as a table.

here is the code

outfile = open(eco_pa + ".txt", "w")

        rows = arcpy.SearchCursor(eco_pa, "OCCUR = 1", "", "COUNT;ECO_ID;SPECIES;OCCUR", "")
        for row in rows:
            SPECIES = row.getValue("SPECIES")
            ECO_ID = row.getValue("ECO_ID")    
            OCCUR = row.getValue("OCCUR")
            COUNT = row.getValue("COUNT")
            
            print SPECIES, ECO_ID, OCCUR, COUNT
            outfile.write(str(SPECIES) + "," + str(ECO_ID) + "," + str(OCCUR) + "," + str(COUNT)

        outfile.close()
Tags (2)
1 Reply
PhilMorefield
Occasional Contributor III
You just need to add a newline character: "\n". Also, you could rewrite the line in question to make it a little cleaner.

# if you're using Python 2.5
outfile.write("%s,%s,%s,%s\n" % (SPECIES, ECO_ID, OCCUR, COUNT))

# if you're using Python 2.6+
outfile.write("{1},{2},{3},{4}\n".format(SPECIES, ECO_ID, OCCUR, COUNT))

# or this...
outfile.write(",".join([SPECIES, ECO_ID, OCCUR, COUNT + "\n"]))
0 Kudos