I am having a little bit of a dilemma. I am having a problem with using getValue() and striping the value that is returned. For instance, take the following code.

import arcpy as ap
mxd = ap.mapping.MapDocument("CURRENT")
mylayer = ap.mapping.ListLayers(mxd, "Structures")[0].name

    for row in ap.SearchCursor(mylayer):

        str =  row.getValue("Struct_ID")
        newstr = str1.lstrip('0')

    if str == "662":

The error I get no matter what is:

Traceback (most recent call last): File "", line 9, in AttributeError: 'NoneType' object has no attribute 'lstrip'

The code works fine if you take out the newstr = str1.lstrip('0') line. The print(str.lstrip('0')) statement works fine. I just cannot use str.lstrip anywhere else in the code other then inside of print.

Anyone know what I am doing wrong or is it something to do with Esri?

  • 1
    Should variable str1 be named str? You have not defined str1 before str1.lstrip('0')
    – klewis
    Oct 30, 2014 at 21:28

1 Answer 1


There are likely empty values in the Struct_ID column, which will come back from getValue as None. This is what the error message is telling you - you got a NoneType object, which does not have an lstrip method, so attempting to call it is a runtime exception. If empty values like this are expected, then protect any code that treats it like a string from working with a None value, like so -

 for row in ap.SearchCursor(mylayer):
    raw_value = row.getValue("Struct_ID")
    if raw_value : # None or a blank string "" will not evaluate to True
       newstr = str1.lstrip('0')
       newstr = "" 
    # newstr is now always a string, regardless of whether getValue returns None or not. 
    # str could still be None.

Also, your remaining code doesn't make sense -

if str == "662":

it's not indented into the for loop, so it can only deal with the last record of the feature class, and if it already equals 662, why would you need to strip non-existent 0's of the left side of it?

  • 2
    I'd like to add str is a keyword in python and and it's best to not reassign it.
    – Paul H
    Oct 30, 2014 at 23:24
  • Thanks for the tip MWrenn. I had a few Null values in the data that I did not know about and that was the issue. Like you said "There is likely an empty value". There were a few null values so it was fixed by checking for Nulls .isNull(). Thank you.
    – GisDoodler
    Oct 30, 2014 at 23:47

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.