I am an beginner in Python programming but I would like to write in only a file .csv (or .txt) the values contained in several rasters.

For example, I built several rasters by using a loop. The rasters are like this:

 outCostPath.save('P_' +  str(int(ID_patch_1)) + '_' + str(int(ID_patch_2)) + ".tif")

For each raster, I would to save the value that is situated at the second line and the column "PATHCOST" in a file .csv. In the end, this file .csv should contain all values contained in all created rasters. Here is my algorithm:

i = 1

for ID_patch_1 = 1 to 100

   for ID_patch_1 = ID_patch_1 + 1 to 100

    ## Building a raster
    fileRaster = outCostPath.save('P_' +  str(int(ID_patch_1)) + '_' + str(int(ID_patch_2)) + ".tif")

    ## Writing in a file .csv
    fileCsv(i,1) = ID_patch_1 ## put value of ID_patch_1 in fileCsv at line i and first column
    fileCsv(i,2) = ID_patch_2 ## put value of ID_patch_2 in fileCsv at line i and second column
    fileCsv(i,3) = fileRaster(2,3) ## put value situated at line 2 and third column in the created raster
    i = i + 1


The problem is that I don't know to code this algorithm in Arcpy.

  • Have you looked at the Table to Table tool? You can export a table to .csv.
    – Aaron
    Commented Sep 7, 2014 at 16:20
  • Hi Pierre, your code looks like visual basic, are you having difficulty with the python language? From what I see you are trying to do a costpath (that bit missing) on patch n and n+1 (your second variable should be ID_patch_2) is that right? and then you want to output the result raster as a CSV in rows/columns? That's going to be a BIG csv file!!! Seriously though, I'd do Raster to point followed by addxy and then get the values from there. Commented Sep 7, 2014 at 22:27


Browse other questions tagged or ask your own question.