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.
Solved! Go to Solution.
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
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
there are a variety of ways, here is one
>>> a = "university of" >>> b = "university of pythonia" >>> >>> a in b True >>> "college" in b False
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
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.
then you could have the best of both worlds
>>> a = "university of pythonia"
>>> "pythonia" in a.split(" ")
True
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
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
thanks everyone. I tried everything on here and the first thing that worked was startswith() so thanks everyone it works perfectly now!