'Python- ArcMap - Calculate Fields
I am python newbie and I am trying to count the number of words in a column (Name) in ArcMap by using
!NAME!.count(' ') + 1
but I run into problems with strings like :
First N' Infant Care Center "Baby World"
type.exceptions.Syntaxerror, even if I use " ",same problem I encounter when I am using other methods like split, strip etc.
Solution 1:[1]
Try
len(!Name!.split(" "))
If that doesn't work...let us know which feature it fails on and maybe more sample data?
Solution 2:[2]
Try encoding the string, arc does funny things with their string encoding...
!NAME!.encode('ascii', 'ignore').count(' ') + 1
Solution 3:[3]
Python can not easily handle mixed double and single quotes, so it's best if you first remove them. One way to do this is to add another field (say newName, calculate it to have the same values as "Name" field, by doing just !NAME!. I am assuming you don't want to alter the Name field. Then within editing mode, use find and replace to replace all quotes " or ' in that new column with nothing (just don't type anything in the replace and run replace all). Now if you use that same approach you used with this new column/field, the problem won't occur. !newName!.count(' ') + 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jimf |
Solution 2 | Pete H |
Solution 3 | Pratik |