Python not recognizing special character in string

3256
8
Jump to solution
10-10-2019 06:26 AM
AlanDodson
New Contributor III

I am using an update cursor to update one field based on the content of another. I can't get part of the Python script I've written to properly handle the underscore special character that is present in my data. I think I need to do something to ensure the string is read as unicode and not ascii, but I cannot figure out how to do this when referring to an index value.  Can someone help?  (Below is not the real script but it is the part that I am having trouble with.)

with arcpy.da.UpdateCursor (in_table="Test", field_names=["CATS", "DOGS"]) as cursor:
    for row in cursor:
        if str(row[0].upper()) not in ["PIZZA", "N_A"]:
            row[1] = 12345
        cursor.updateRow(row)
del cursor, row‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

If you are in ArcMap and you type an underscore into an attribute field, that should be the same underscore in your code snippet.  I am not sure why isn't working.  Can you build an example table the has the same issues that you could upload and share?

View solution in original post

8 Replies
JoshuaBixby
MVP Esteemed Contributor

A couple of questions....  First, field names are not allowed to start with a number, so I am unclear how you are working with a field named, "123".  Second, on Line #03, you have mixed parentheses/brackets on the right, so that line should be generating an error.

0 Kudos
AlanDodson
New Contributor III

The first error you found was from me being sloppy when anonymizing my fields for posting here. The second error was a typo when moving the snippet to a computer connected to the outside world.  I've edited the code above to fix those mistakes. Good catches but the real script (of which this is a generic snippet) runs fine without errors, but doesn't do what I expect it to if the underscore character is present. If I amend my data to be "NA" and my script to look for "NA" it works as expected as an error check, but unfortunately editing the source data when doing the real work is not an option at the moment.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you sure the field is just "N_A" and doesn't have a white space character or some other non-printable character?

If the field value is just "N_A" or "n_a", your statement should catch it:

>>> s = "n_a"
>>> str(s.upper()) in ["PIZZA", "N_A"]
True
>>> str(s.upper()) not in ["PIZZA", "N_A"]
False
>>> ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

UPDATE:  It just dawned on me that the underscore might be a different underscore than you think, e.g., a combining low line or full-width low line.  Are you running this in ArcMap or ArcGIS Pro?

AlanDodson
New Contributor III

I’m working in ArcMap 10.3.1. If I type in an underscore in my attribute field and type in the same underscore (or copy-paste it into the code) would that ensure it’s the same character?   I’ll keep playing with this and I’ll check for white space. I appreciate your help!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you are in ArcMap and you type an underscore into an attribute field, that should be the same underscore in your code snippet.  I am not sure why isn't working.  Can you build an example table the has the same issues that you could upload and share?

AlanDodson
New Contributor III

I'll see what I can do on Friday.  Thank you for your insight so far

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Alan Dodson‌, I see you have been going through this discussion and marking helpfuls.  Did you get it sorted out?  If so, please share and mark a response correct or assumed answered to close it out.

0 Kudos
AlanDodson
New Contributor III

Sorry for the late reply to this - I was moved to another priority job and just got back to my normal work today.  I’ve tried things again and as far as I can tell it is working as expected with no changes.  Perhaps I just needed to clear some cobwebs from my understanding of the issue.  Regardless, thanks for helping out.  

0 Kudos