1

I like to create a table with open() in a loop using the SearchCursor and InsertCursor, but I don't know how. Before I used a few geoprocessing tools to get a field for percent. The Attributtable of the raster looks like this

enter image description here

So I need to write the field percent with the value 8 in a new table to get a result which looks like this:

enter image description here

This is where I'm working on with ArcMap10.2.1:

import arcpy
import os
from arcpy import env

env.overwriteOutput = True
env.workspace = r"D:\Julia_T\projekt\testdataA"

#folder to list
ascFileList = arcpy.ListFiles("*.asc")

print ascFileList
#works
for ascFile in ascFileList:
# geoprocessing steps
ascFileName = os.path.splitext (ascFile)[0]
print ascFileName
#works
rastFile = ascFileName + "_output.img"
print rastFile
#works
#run the tool
arcpy.ASCIIToRaster_conversion (env.workspace + "\\" + ascFile, env.workspace + "\\" + rastFile, "INTEGER")

# Process: Projektion definieren
arcpy.DefineProjection_management (env.workspace + "\\" + rastFile, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
# Process: Feld hinzufügen
arcpy.AddField_management (env.workspace + "\\" + rastFile,"Prozent","DOUBLE")
# Process: Feld berechnen
arcpy.CalculateField_management(env.workspace + "\\" + rastFile,"Prozent","( [Count] /53959) *100", "VB")
#tabelle bauen
with arcpy.da.SearchCursor(env.workspace + "\\" + rastFile,["Value","Prozent"],""""Value" = 8""") as sCur:
    outputfile = r"D:\Julia_T\output\output.txt"
    with arcpy.da.InsertCursor(outputfile,["Value","Prozent"]) as iCur:
               for row in sCur:
                print row [0], row [1]
                f = open (outputfile, 'w')
                f.write ("Value\nProzent")
                f.close()

How can I get the percent from the value 8 in a new table?

  • Are you opening and closing the file inside the for loop, or is that an indentation error? – Paul Jul 28 '14 at 13:40
4

An InsertCursor can write to a Table, your text file is not a Table! Also you create iCur but never actually do anything with it...

Try this code:

outputfile = r"D:\Julia_T\output\output.txt"
f = open (outputfile, 'w')
f.write ("Value,Prozent")
with arcpy.da.SearchCursor(env.workspace + "\\" + rastFile,["Value","Prozent"],""""Value" = 8""") as sCur:
    for row in sCur:
        print row [0], row [1]            
        f.write(str(row[0]) + "," + str(row[1]) + "\n")
f.close()
  • Yes my code doesn’t look well. I tried it yesterday a few hours and deleted a few lines of the code while getting nervous. Anyway, thanks a lot for your help. It works but the code gives just the values from the last file from the list back. So I think it overwrites the other results from the searchCursor. Do I have to use something like a next operation? – Nora Jul 29 '14 at 6:55
  • I have tweaked the code slightly have a look at it now but your cursor has a whereclause of Value = 8 so this will only every return a single row from the raster. Your original uploaded code appears to have lost its indentation so it's at all clear what the loop structure is. – Hornbydd Jul 29 '14 at 8:51
  • So I have to export the attribute table with the values (see the picture in the question) first and loop than through each attribute table with the searchCoursor in a new script by creating a new List? The whereclause of Value = 8 is just to create a new table with just the percent of the value 8 At the moment the code givs me this back: Value,Prozent,8,94.1010767435 – Nora Jul 29 '14 at 9:23
  • Perhaps this is my solution: outputpath = r"D:\Julia_T\output" outname = rastFile + "table.dbf" arcpy.TableToTable_conversion(env.workspace + "\\" + rastFile, outputpath, outname) And than I have to continue in a new script. – Nora Jul 29 '14 at 10:04
0

At the end the script is working with writing one open-operation at the front of the script and one at the end. The one at the end has to have a open(xxx.txt, 'a'):

import arcpy
import os
from arcpy import env

env.overwriteOutput = True
env.workspace = r"D:\Julia_T\projekt\testdataA"

#folder to list
ascFileList = arcpy.ListFiles("*.asc")

print ascFileList

outputfile = r"D:\Julia_T\output\output.txt"
f = open (outputfile, 'w')
f.write ("Value,Prozent,\n")
f.close()
#works
for ascFile in ascFileList:
# geoprocessing steps
ascFileName = os.path.splitext (ascFile)[0]
print ascFileName
#works
rastFile = ascFileName + "_output.img"
print rastFile
#works
#run the tool
arcpy.ASCIIToRaster_conversion (env.workspace + "\\" + ascFile, env.workspace + "\\" + rastFile, "INTEGER")

# Process: Projektion definieren
arcpy.DefineProjection_management (env.workspace + "\\" + rastFile, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
# Process: Feld hinzufügen
arcpy.AddField_management (env.workspace + "\\" + rastFile,"Prozent","DOUBLE")
# Process: Feld berechnen
arcpy.CalculateField_management(env.workspace + "\\" + rastFile,"Prozent","( [Count] /53959) *100", "VB")
#start table

f = open (outputfile, 'a')
with arcpy.da.SearchCursor(env.workspace + "\\" + rastFile,["Value","Prozent"],""""Value" = 8""") as sCur:
    for row in sCur:
        print row [0], row [1]
        f.write(str(row[0]) + "," + str(row[1]) + "\n")
f.close()

`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.