Update date field

2680
8
Jump to solution
01-29-2018 03:00 PM
LauraBusolo2
New Contributor III

I'm pretty new to python and I have a table in a geodatabase that contains info on data sources, names, targets and a date field. I have a script that copies features based on a user's input, e.g. if ft_ID ==1. then copy features. I want to update the date field based on the affected/copied features row that the "copy features" was executed, and I don't know how to. (e/g. If I copy feature with ID as "1", update the corresponding date field for today/whatever day I copy that field). I just know I may need to use an update cursor.

I was playing around with the update cursor, but only got it to work to update current date for all features. This is how it looks like so far.

LYR_UPDATE_TABLE = r".\...gdb\Layer_Update"
#FIELD_NAMES = ['a', 'b', 'c',  'LastUpdate']
fld = 'LastUpdate'
btch_num = raw_input ("Please enter ID: ")

with arcpy.da.UpdateCursor (LYR_UPDATE_TABLE, fld) as dt_cursor:
   for row in dt_cursor:
      # btch_id was defined earlier as the user's input to choose the features to cpy from the table
      if batch_id == int(btch_num):
      #Update the LastUpdate field with the date of the copy features-update
      """row [6] = (...)"""
      dt_cursor.updateRow([datetime.date.today()])
      print ('date Update Success!')
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You created the update cursor with a single field, i.e., fld.   In Line 16, you are trying to unpack a single item tuple into 7 different variables, hance the error that you need more than 1 value to unpack.  Instead of passing fld to the cursor, pass FIELD_NAMES.

View solution in original post

8 Replies
JoshuaBixby
MVP Esteemed Contributor

Are you getting an error, is so, what?  Provide the whole traceback.

LauraBusolo2
New Contributor III

The error was:

ValueError: need more than 1 value to unpack

0 Kudos
RandyBurton
MVP Alum

Usually the error will also indicate a line number and the module where the script had problems.  The complete error message block would help.

In the meantime, you might try something like:

LYR_UPDATE_TABLE = r"C:\Path\to\file.gdb\Layer_Update"

fld = 'LastUpdate'

btch_num = raw_input("Please enter a valid Batch ID: ") #assuming you are outside arcmap

where_clause = "batch_id = '{}'".format(btch_num) # where will limit to matching rows

with arcpy.da.UpdateCursor (LYR_UPDATE_TABLE, fld, where_clause) as dt_cursor:
    for row in dt_cursor:
        row[0] = datetime.datetime.strftime(datetime.date.today(),"%Y-%m-%d")
        dt_cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I am assuming you are using a file geodatabase, and working outside ArcMap.  You can limit the rows in the cursor with a where clause.  Line 11 shows how you can format your date.  Hope this helps.

Edit:

For the where clause, if the field type is numeric, omit the single quotes around the field value:

where_clause = "batch_id = {}".format(btch_num) # where will limit to matching rows
LauraBusolo2
New Contributor III

The error is related to the line with the row.

See below:

File "testscript.py", line 16, in <module>
batch_id, source_path, source_name, target_path, target_name, method, last_update= row  #This is line 16, which reflects variables for the field names in the table
ValueError: need more than 1 value to unpack

0 Kudos
DanPatterson_Retired
MVP Emeritus

from this line

batch_id, source_path, source_name, target_path, target_name, method, last_update= row

7 values were expected... row .... provided 1. which I suspect was empty.

As a check, put...

print(row) immediately before that line and see if it prints out 7 values, if not, then there is something wrong with the input data.  Certainly there is something wrong with the code logic since that case should be checked for and accounted for in the script

row = [1, 2, 3, 4, 5, 6, 7]

a, b, c, d, e, f, g = row

print(row)
[1, 2, 3, 4, 5, 6, 7]

print(a, b, c, d, e, f, g)
1 2 3 4 5 6 7

row = [1, 2, 3]

# ---- how to check
if len(row) == 7:
    print('good')
else:
    print('not enough values{}'.format(row))
    
not enough values[1, 2, 3]
JoshuaBixby
MVP Esteemed Contributor

You created the update cursor with a single field, i.e., fld.   In Line 16, you are trying to unpack a single item tuple into 7 different variables, hance the error that you need more than 1 value to unpack.  Instead of passing fld to the cursor, pass FIELD_NAMES.

XanderBakker
Esri Esteemed Contributor

As a side note... if you enable editor tracking ArcGIS will maintain a date field and editor field to store the last edit: Enable Editor Tracking—Data Management toolbox | ArcGIS Desktop 

RandyBurton
MVP Alum

In the field list (commented out) you have "BatchID"  and in your code you are testing with "if batch_id == ..."  If these are field names, they are not the same field.  Did you mean them to be the same?

0 Kudos