# Convert Raster into Cell by Cell Table

I'm hoping to run some Python mathematics on a raster, outside of ArcGIS. It's important to have a cell by cell table for the analysis I want to run. I plan to store it in a python dictionary similar to this:

``````Dict = {
row1:[0,1,0,2,1,0,1,1,1,3],
row2:[3,1,0,3,1,0,0,1,0,3],
row3:[0,1,1,2,3,0,1,2,1,3]}
``````

Obviously this is just and example, but this is what I want to turn my raster into. I always see these diagrams within the ArcGIS help:

And this is exactly what I would like out of a raster. I can easily go from a CSV to the dictionary I want, but I'm having trouble getting a raster to a cell by cell table.

## 1 Answer

Referencing the question you linked. This code should work for ya:

``````import arcpy
fOut = open('outputFile.py', w)                 # Open output python file
fOut.write('Dict = {' + '\n')                   # Write the dictionary
rstArray = arcpy.RasterToNumPyArray(rasterFile) # Change rasterFile to numpy array
rows, cols = rstArray.shape                     # Return the rows, columns
for rowNum in xrange(rows):                     # Loop through the rows
fOut.write('row' + str(rowNum) + ':[')
for colNum in xrange(cols):                 # Loop through the row's columns
value = rstArray.item(rowNum, colNum)   # Get the value at the cell
fOut.write(str(value) + ',')            # Write the value
fOut.write('],' + '\n')                     # End the list and start new item
fOut.write('\t' + '}')                          # End the dictionary
fOut.close()
``````

I've tested it out on a small raster and it works to provide an output like you specified about. If you have a large raster this is going to get hard to read with python quickly. Look at indexing similar values.