I need help on the Update Cursor function

6958
42
Jump to solution
08-23-2012 07:27 AM
OLANIYANOLAKUNLE
Frequent Contributor
Im trying to update a field called "GENERATED" with an attribute called "GENERATED" every time i autogenerate a map, i constructed this script and it is not updating and i didnt see an error(s)

rows = arcpy.UpdateCursor("Parcels",'[OBJECTID] = "1"')
row = rows.next()
while row:
    row.setvalue(GENERATED, GENERATED)
    rows.updateRow(row)
    row = rows.next()

Please any suggestions. Im desperate. Thanks
Tags (2)
0 Kudos
42 Replies
ChrisSnyder
Honored Contributor
Don't include the SQL expression in your cursor statement... If your cursor is only returning records where "TDP_Status = 'GENERATED'", how would your else statement ever work... Do you see the issue?
0 Kudos
curtvprice
MVP Alum

Where do i add the field?


arcpy.AddField("table","TIME_GEN","TEXT","","",20)
import datetime
now = datetime.datetime.now()
strNow = now.strftime("%d %b %Y %H:%M:%S")
arcpy.CalculateField_management("table","FIXDATE",'"' + strNow + '"')
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks curtvprice for the nugget of information, please can you help me with this
Please can you kindly help with this script also; i am trying to do a search through my attribute table, for a field called TDP_Status and if the attribute is GENERATED, I want the arcpy script to print TDP ALREADY GENERATED and stop the process else it should continue with the buffer analysis. This is my small script


generatedFieldName = "TDP_Status"
rows = arcpy.SearchCursor("Parcels", '"TDP_Status" = \'GENERATED\'')
for row in rows:
if row.TDP_Status = 'GENERATED'
print TDP ALREADY GENERATED
else:
arcpy.Buffer_analysis ("Parcels", "ClipFeature", "12.5 meters", "FULL", "ROUND", "LIST")

Any suggestions?
0 Kudos
curtvprice
MVP Alum
Thanks curtvprice for the nugget of information, please can you help me with this
Please can you kindly help with this script also; i am trying to do a search through my attribute table, for a field called TDP_Status and if the attribute is GENERATED, I want the arcpy script to print TDP ALREADY GENERATED and stop the process else it should continue with the buffer analysis.


Please [post=224499]use [noparse]
 blocks[/noparse][/post] in your posts - it really helps.

No need for a cursor - the GetCount tool is very helpful here.

arcpy.MakeFeatureLayer_management("Parcels", "tLyr", '"TDP_Status" = \'GENERATED\'')
count = int(arcpy.GetCount_management("tLyr").getOutput(0))
if count == 0: 
    arcpy.AddMessage("TDP already generated")
else:
    arcpy.Buffer_analysis("Parcels", "ClipFeature", "12.5 meters", "FULL", "ROUND", "LIST")
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Sorry to bother you again i got this error when i tried to run the script after i changed the following

1. arcpy.MakeFeatureLayer("Parcels", "tLyr", '"TDP_Status" = \'GENERATED\'')
MakeFeatureLayer_Management

2. count = int(arcpy.GetCount("tLyr").GetOutput(0))
'GetCount_Management'

 File "<string>", line 1, in <module>
AttributeError: 'Result' object has no attribute 'GetOutput'


Please what do you think
0 Kudos
curtvprice
MVP Alum
Sorry to bother you again i got this error


Sorry; I fixed the code above. There's an example for getOutput (I spelled it wrong) in the help for the GetCount tool.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Sorry; I fixed the code above. There's an example for getOutput (I spelled it wrong) in the help for the GetCount tool.


Thanks curtvprice, you've really helped me.

Please can you help me with this?

i want to generate a pdf output by using the arcpy.exporttopdf function, i want the final output to use this nomenclature for naming the pdf output (plot_no.pdf); the plot_no would be derived from an attribute value under the field named plot_no in the attribute table. Please do you have any suggestions.

Thanks
0 Kudos
curtvprice
MVP Alum
i want to generate a pdf output by using the arcpy.exporttopdf function, i want the final output to use this nomenclature for naming the pdf output (plot_no.pdf); the plot_no would be derived from an attribute value under the field named plot_no in the attribute table. Please do you have any suggestions.


OK, last post on this thread. 🙂

Extract the value from your table using a cursor (row.getValue), convert it to a string if necessary and construct a pathname to pass to the arcpy.ExportToPDF tool -- like this:

outName = "plot_" + str(val) + ".pdf"
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
1. Please can you shed more light on how to extract the value from your table using a cursor (row.getValue) and the conversion, im still a juvenile with arcpy/python.

2. For a reason i don't know the script

arcpy.MakeFeatureLayer_management("Parcels", "tLyr", '"TDP_Status" = \'GENERATED\'')
count = int(arcpy.GetCount_management("tLyr").getOutput(0))
if count == 0: 
    arcpy.AddMessage("TDP already generated")
else:
    arcpy.Buffer_analysis("Parcels", "ClipFeature", "12.5 meters", "FULL", "ROUND", "LIST")


Continues with the buffer process even if the count is o or 1. Do you know why?
0 Kudos
curtvprice
MVP Alum
1. Please can you shed more light on how to extract the value from your table using a cursor (row.getValue) and the conversion, im still a juvenile with arcpy/python.


There are helpful examples in the help for SearchCursor.

2. For a reason i don't know the script

arcpy.MakeFeatureLayer_management("Parcels", "tLyr", '"TDP_Status" = \'GENERATED\'')
count = int(arcpy.GetCount_management("tLyr").getOutput(0))
if count == 0: 
    arcpy.AddMessage("TDP already generated")
else:
    arcpy.Buffer_analysis("Parcels", "ClipFeature", "12.5 meters", "FULL", "ROUND", "LIST")


Continues with the buffer process even if the count is o or 1. Do you know why?


This shouldn't happen if your indents are correct as above. I would test your script by printing a test message (arcpy.AddMessage("Count is: " + str(count)).
0 Kudos