update cursor using a wildcard

2464
8
Jump to solution
03-17-2016 12:32 PM
jameljoseph
New Contributor II

hello,

I am using an update cursor in my python script to go through my table records and I want to assign the same value to all the universities that start with "University of".

Here is an example of what I want to do:

if row[0] == "Boston College":

[code here]

elif row[0] == "University of *":

[code here]

else:

I thought the * was the wildcard but all my records that have "University of" as the prefix in a record do not have any values in them, whereas Boston College does.

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

For your purposes, 'in' should work, however it doesn't guarantee that the string starts with those characters. I'll just point out that there is 'startswith()' that tests string beginnings:

>>> a = "university of"  
... b = "university of pythonia"
... c = "pythonia university of"
... print b.startswith(a) # does b start with a?
... print c.startswith(a) # does c start with a?
... 
True
False

or, an alternative using slice notation:

>>> a = "university of"  
... b = "university of pythonia"
... c = "pythonia university of"
... print b[:len(a)] == a # do the first few items (letters) in b equal a?
... print c[:len(a)] == a # do the first few items (letters) in c equal a?
... 
True
False

View solution in original post

8 Replies
roemhildtg
Occasional Contributor III

The star operator only works for the UpdateCursor fields parameter. I think in this case you can use the `in` operator:

elif 'University of' in row[0]:

     #do stuff

0 Kudos
DanPatterson_Retired
MVP Emeritus

there are a variety of ways, here is one

>>> a = "university of"
>>> b = "university of pythonia"
>>> 
>>> a in b
True
>>> "college" in b
False
0 Kudos
DarrenWiens2
MVP Honored Contributor

For your purposes, 'in' should work, however it doesn't guarantee that the string starts with those characters. I'll just point out that there is 'startswith()' that tests string beginnings:

>>> a = "university of"  
... b = "university of pythonia"
... c = "pythonia university of"
... print b.startswith(a) # does b start with a?
... print c.startswith(a) # does c start with a?
... 
True
False

or, an alternative using slice notation:

>>> a = "university of"  
... b = "university of pythonia"
... c = "pythonia university of"
... print b[:len(a)] == a # do the first few items (letters) in b equal a?
... print c[:len(a)] == a # do the first few items (letters) in c equal a?
... 
True
False
BlakeTerhune
MVP Regular Contributor

I second Darren Wiens in using startswith()

if row[0] == "Boston College":
    # [code here]
elif row[0].startswith("University of "):
    # [code here]
else:
    # [code here]

Just make sure the field you're doing this on has only strings (text). The code above will error if it hits a number or date value.

0 Kudos
DanPatterson_Retired
MVP Emeritus

then you could have the best of both worlds

>>> a = "university of pythonia"

>>> "pythonia" in a.split(" ")

True

0 Kudos
DarrenWiens2
MVP Honored Contributor

I'll play along. If you're determined to use split to test a string beginning...

>>> a = "university of" 
... b = "university of pythonia"
... c = "pythonia university of"
... print b.split(a)[0] == '' # if b is split by a, is the first item in the returned list blank?
... print c.split(a)[0] == '' # if c is split by a, is the first item in the returned list blank?
...
True
False
0 Kudos
DanPatterson_Retired
MVP Emeritus

  I will work on some numpy examples then

Update

2D question demonstrated...extendable to N dimensions

>>> arr
array([['university'],
       ['of'],
       ['pythonia']], 
      dtype='|S10')
>>> np.any(np.where(arr=="pythonia",True,False))
True
0 Kudos
jameljoseph
New Contributor II

thanks everyone.  I tried everything on here and the first thing that worked was startswith() so thanks everyone it works perfectly now!

0 Kudos