How to remove first character from every row of an attribute field

3081
4
Jump to solution
08-06-2013 06:53 AM
BrendanWhite1
New Contributor
Hello all,

I am new to Python and, as you can probably guess, I am having issue with some code I'm attempting to write.  The following will describe what I am attempting to do, and hopefully you wonderful people can tell me what I'm doing wrong.

I have a point feature class with an attribute field called "STOINLET_I."  The field looks like this, and has about 2000 entries (rows):

PDIN-0001
PDIN-0002
PDIN-0003
PDIN-0004
...

Here is a screenshot, to help better articulate the feature class:
[ATTACH=CONFIG]26495[/ATTACH]

What I am attempting to do with this script is remove the first character in every row of the field so that the "P" no longer exists, which would leave me with this:

DIN-0001
DIN-0002
DIN-0003
DIN-0004
...

I am confident that the first section of code runs successfully.  The problem lies within the cursor, but I cannot figure out where.  I know this should be easy, but any help would be sincerely appreciated!

import arcpy, string, os, fileinput from arcpy import env env.workspace = "desired workspace" env.overwriteOutput = True fc = "desired feature class" verification = arcpy.Exists (fc) print verification del verification   cursor = arcpy.da.UpdateCursor (fc, ["STOINLET_I"]) for row in cursor:     row.Replace(row[0:],'') del row del cursor
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
cursor = arcpy.da.UpdateCursor (fc, ["STOINLET_I"]) for row in cursor:     cursor.updateRow([row[0][1:]])

View solution in original post

0 Kudos
4 Replies
JimCousins
MVP Regular Contributor
You have trouble with this line:
row.Replace(row[0:],'')
You are getting the whole string with [0:]. Based on the sample data, use:
row.Replace('P', '')
Regards,
Jim
0 Kudos
BrendanWhite1
New Contributor
Thanks for such a quick response, Jim. I ran the code with Jim's suggestion, and the code is still kicking up an error, seen here (shell text from IDLE):

True

Traceback (most recent call last):
File "filepath/PythonScript.py", line 16, in <module>
row.Replace('P','')
AttributeError: 'list' object has no attribute 'Replace'


Based upon a quick google search it seems as though people receive this error when their indentation is off, or when filepaths are messy. I managed to account for the filepath with the "verification =" line of code. The blue text reading "true" above confirms the filepath. Also, the spacing in this code is not complicated, and from what I can tell, is correct. Are there any suggestions on how to alleviate this error?
0 Kudos
JasonScheirer
Occasional Contributor III
cursor = arcpy.da.UpdateCursor (fc, ["STOINLET_I"]) for row in cursor:     cursor.updateRow([row[0][1:]])
0 Kudos
BrendanWhite1
New Contributor
cursor = arcpy.da.UpdateCursor (fc, ["STOINLET_I"])
for row in cursor:
    cursor.updateRow([row[0][1:]])


THIS WORKED! Thank you so much.
0 Kudos