Use split field to update a new field

850
7
04-28-2011 05:31 AM
StuartGibson
New Contributor
Hi all,

I am having problems with what should be something really simple.  I have a text field ("Start_Time") formatted as 11:23:40:60.  I am trying to split the text at ':' and write the first set to a new "Hour" field.  I can get the split to work but am having problems getting it into the "Hour" field.  I've tried it a couple of different ways and neither of them are working.  I've tried:
    UpCur = gp.UpdateCursor(InFC)
    row = UpCur.Next()
    while row:
        TimeList = row.Start_Time.split(":")
        hr = TimeList[0]
        row.setvalue("Hour", hr)
        row = UpCur.next()

and I've also tried:
    UpCur = gp.UpdateCursor(InFC)
    row = UpCur.Next()
    while row:
        row.Hour = row.Start_Time.split(":")[0]
        row = UpCur.next()


I must be missing something simple but can't figure it out.  I'm using ArcMap 9.3.1 and python 2.5.  Thanks in advance.

Stu
Tags (2)
0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus
what version of arcmap?
is the destination field a text field?  It should be since splitting a strings results in string, if you want numbers, then you need to "int" the resultant split if placing in a short or long numeric field
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how to do this.  You will need to replace 'arcpy' with 'gp'.

input = "<table/feature class>"

rows = arcpy.SearchCursor(input)

for row in rows:
    TimeList = row.Start_Time.split(":")
    rows2 = arcpy.UpdateCursor(input)
    for row2 in rows2:
        row2.Hour = TimeList[0]
        rows2.updateRow(row2)


The field 'Hour' is of type Text.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is the code I got to work at 10.  You should be able to replace 'arcpy' with 'gp' and this should work at 9.3.

input = "<table/feature class>"

rows = arcpy.UpdateCursor(input)
for row in rows:
    TimeList = row.Start_Time.split(":")
    row.Hour = TimeList[0]
    rows.updateRow(row)

del row, rows
0 Kudos
StuartGibson
New Contributor
I'm using ArcMap 9.3.1 (build 3500).

I originally had the "Hour" field as an interger and tried to using
 row.Hour = int(row.Start_Time.split(":")[0])

but when that wasn't working, I deleted the "Hour" field and recreated it as a text field just to try and get something working.
0 Kudos
StuartGibson
New Contributor
Here is the code I got to work at 10.  You should be able to replace 'arcpy' with 'gp' and this should work at 9.3.


I get this error when running it after switching out for gp:

Traceback (most recent call last):
  File "C:/Temp/ESRI_Temp/Development/time_search2.py", line 12, in <module>
    for row in rows:
TypeError: 'geoprocessing cursor object' object is not iterable

import arcgisscripting
import string
gp = arcgisscripting.create(9.3)

inFC = "C:\\Temp\\ESRI_Temp\\Development\\sk_TEST.shp"

rows = gp.UpdateCursor(inFC)
for row in rows:
    TimeList = row.Start_Time.split(":")
    row.Hour = TimeList[0]
    rows.updateRow(row)
del row, rows
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following:

import arcgisscripting
gp = arcgisscripting.create(9.3)

input = "<shapefile>"

rows = gp.UpdateCursor(input)
row = rows.Next()
while row:
    TimeList = row.Start_Time.split(":")
    row.Hour = TimeList[0]
    rows.UpdateRow(row)
    row = rows.Next()

del row, rows
0 Kudos
StuartGibson
New Contributor
Try the following:

import arcgisscripting
gp = arcgisscripting.create(9.3)

input = "<shapefile>"

rows = gp.UpdateCursor(input)
row = rows.Next()
while row:
    TimeList = row.Start_Time.split(":")
    row.Hour = TimeList[0]
    rows.UpdateRow(row)
    row = rows.Next()

del row, rows


That works.  Thanks everyone for the help!!!  Is there a specific reason why the other methods weren't working?  I'm still a relative newbie with this, and am trying to understand why this worked and the other 'while' and 'for' loops didn't. 

Stu
0 Kudos