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

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 24, 2019 at 17:51
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)
answered Oct 25, 2019 at 22:26
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')

answered Oct 28, 2019 at 8:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.