I am trying to pull items out of an attribute table. I want to pull out all county names that are in capital letters. I was using UPPER (County) = 'ANDREWS' but it was only pulling up Andrews. I need to pull up several different named counties.
Solved! Go to Solution.
No on to what you have to do... since I don't think you can do the query all at once.
Add a field to contain a string or boolean
Calculate the new field using
allcaps(!YourFieldNameHere')
and calculate into a boolean or string field. then you have a field for querying.
def allcaps(a):
"""check for all caps"""
uc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
n = len(a)
m = sum([i in uc for i in a])
return n == m
# ---- test case
a = 'ANDREWS'
allcaps(a)
True
b = 'AnDrews'
allcaps(b)
False
python lesson
I think the field calculator/query stuff supports the 'IN' operator.. what you want is to compare to the first value of the string to the capital letters in a list
A demo, but this can easily be made into a function if you don't want to copy ucl over obviously so your expression would be
'Yourfield'[0] in uc # or whatever surrounding characters are used for field names
uc = string.ascii_uppercase
# ---- make a list for future reference
ucl = list(uc)
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# ucl = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# --- a test
a = 'ANDREWS' # now 'a' would be your field
a[0] in ucl
True
a[1] in ucl
True
# ---- a quick test for lowercase
'b' in ucl
False
So there is not a simple syntax for inside the select by attributes??
No on to what you have to do... since I don't think you can do the query all at once.
Add a field to contain a string or boolean
Calculate the new field using
allcaps(!YourFieldNameHere')
and calculate into a boolean or string field. then you have a field for querying.
def allcaps(a):
"""check for all caps"""
uc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
n = len(a)
m = sum([i in uc for i in a])
return n == m
# ---- test case
a = 'ANDREWS'
allcaps(a)
True
b = 'AnDrews'
allcaps(b)
False
Bummer!! Thanks for the help.
What if you calculated the names to be ALL UPPER CASE or All Proper Case? (Hmmmm.... sounds dangerously like a standard....)
Using the Python parser in the Field calculator:
(Pre-Logic Script Code box)
def myUpper(instring):
x = instring.upper()
return x
myUpper(!YourFieldName!)
changed slightly:
def myProper(instring):
x = instring.title()
return x
myProper(!YourFieldName!)
I started with the field value of jose diablo and get JOSE DIABLO and Jose Diablo respectively....
as long as you leave the original field alone... and calculate in a new field... then you would have a choice... but what if they were to be all lower... or upper then lower... the possibilities are endless... there are so many 'standards' to choose from as one of our illustrious compatriots said
I'm an upper case kind of guy. The data bases I manage are UC. Nothing else....
I am too much pythonic... all lower case for me