1

I have a shapefile with an attribute table that partially looks like this:

Row | Shape | Date
0   | Poly  | 05-Sep-10 16:12:00
1   | Poly  | 06-Sep-10 17:23:01

Is there a simple method that I can loop through the 'Date' column and add a 20 before the 10 in each cell? So that the output looks like this:

Row | Shape | Date
0   | Poly  | 05-Sep-2010 16:12:00
1   | Poly  | 06-Sep-2010 17:23:01

Date fields are text format.

I have been able to hand edit multiple rows, but let's say I have 10,000 rows, this would take a considerable amount of time.

Attempt using ArcPy in ArcMap:

import arcpy

path='C:/path/to/file.dbf'

field='Date' #name of the second column

twenty = '20'

with arcpy.da.UpdateCursor(path, field) as cursor:

    for row in cursor:
        new_row = row[0][:7] + twenty + row[0][7:]
        row = new_row
        print(row)
        cursor.updateRow(row)

Error I receive with this is:

TypeError: sequence of size must match the size of the row

0

2 Answers 2

1

Try changing:

row = new_row

to:

row = (new_row)

or:

row = [new_row]

I suspect your cursor needs either a list or tuple returned to it. That list or tuple will contain your single string.

Personally, I would do it, as @smiller commented, using something more like:

with arcpy.da.UpdateCursor(path, field) as cursor:
    for row in cursor:
        row[0] = "{}20{}".format(row[0][:7],row[0][7:])
        print(row)
        cursor.updateRow(row)
0
1

I think the pythonic (and universal) way of doing this is to convert those dates from string to datetime object and reformatting by using string directives. In your case, in a new field and using field calculator by Show Codeblock option:

Pre-logic script:

import datetime

new_field =

datetime.datetime.strptime(!Date!,'%d-%b-%y %H:%M:%S').strftime('%d-%b-%Y %H:%M:%S')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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