Hello All,
I'm trying to update the value in a specific field ('uid_n') in the last row of a table with a value read out of a text file. I've figured out how to return the last value in the table for the uid_n field, but am struggling with the actual update bit. Does anyone have any ideas? Any feedback is greatly appreciated!
My code so far:
import arcpy from arcpy import env import os # open arcpar.txt arcpar = open(r'C:\Users\admin\Desktop\ToeB 10.2\arcpar.txt') # read UID line line = arcpar.readlines()[1] UID = line[4:-1] # set workspace for arcpy env.workspace = r'C:\Users\admin\Desktop\ToeB 10.2\Data Svenja\Toeb Svenja.gdb' # possible feature classes punkte = 'Punktobjekt' # field to use to find last record in table objectId = 'OBJECTID_1' returnField = ["OBJECTID_1", "uid_n"] sql_clause = (None,'ORDER BY {} DESC'.format(objectId)) last_row = '' last_OID = '' # function to copy text to clipbard def addToClipBoard(text): command = 'echo ' + text.strip() + '| clip' os.system(command) # call function to copy UID from arcpar.txt to clipboard addToClipBoard(UID) with arcpy.da.UpdateCursor(punkte, returnField, where_clause=None, sql_clause=sql_clause) as cursor: last_row = cursor.next() lastUID = last_row[1]
Solved! Go to Solution.
The error says 'row' is not defined because in Mitch's code there is no variable 'row', hence it is undefined when you try to update the cursor based on a non-existent variable. I am not sure I completely understand what value you are trying to use to update the row with, but does the following work:
with arcpy.da.UpdateCursor(punkte, returnField, where_clause=None, sql_clause=sql_clause) as cursor: last_row = cursor.next() last_row[1] = UID cursor.updateRow(last_row)
Have you tried updating the row with 'cursor.updateRow(row)'?
Hi Mitch, thanks for the quick feedback. When I run that I get an error: 'row' is not defined.
The error says 'row' is not defined because in Mitch's code there is no variable 'row', hence it is undefined when you try to update the cursor based on a non-existent variable. I am not sure I completely understand what value you are trying to use to update the row with, but does the following work:
with arcpy.da.UpdateCursor(punkte, returnField, where_clause=None, sql_clause=sql_clause) as cursor: last_row = cursor.next() last_row[1] = UID cursor.updateRow(last_row)
Hi Joshua, yeah, that's exactly what I was looking for, I was missing the [1] in there. Thanks!