Whitty21 Whitty21 - 1 month ago 9
Python Question

Looking for numeric characters in a single word string PYTHON

I have a variety of values in a text field of a CSV

Some values look something like this
AGM00BALDWIN
AGM00BOUCK

however, some have duplicates, changing the names to
AGM00BOUCK01
AGM00COBDEN01
AGM00COBDEN02

My goal is to write a specific ID to values NOT containing a numeric suffix

Here is the code so far

prov_count = 3000
prov_ID = 0
items = (name, x, y)
xy_tup = tuple(items)

if "*1" not in name and "*2" not in name:
prov_ID = prov_count + 1
else:
prov_ID = ""


It seems that the the wildcard isn't the appropriate method here but I can't seem to find an appropriate solution.

Answer

You can use slicing to find the last 2 characters of the element and then check if it ends with '01' or '02':

l = ["AGM00BALDWIN", "AGM00BOUCK", "AGM00BOUCK01", "AGM00COBDEN01", "AGM00COBDEN02"]

for i in l:
    if i[-2:] in ('01', '02'):
        print('{} is a duplicate'.format(i))

Output:

AGM00BOUCK01 is a duplicate
AGM00COBDEN01 is a duplicate
AGM00COBDEN02 is a duplicate

Or another way would be using the str.endswith method:

l = ["AGM00BALDWIN", "AGM00BOUCK", "AGM00BOUCK01", "AGM00COBDEN01", "AGM00COBDEN02"]

for i in l:
    if i.endswith('01') or i.endswith('02'):
        print('{} is a duplicate'.format(i))

So your code would look like this:

prov_count = 3000
prov_ID = 0
items = (name, x, y)
xy_tup = tuple(items)

if name[-2] in ('01', '02'):
    prov_ID = prov_count + 1
else:
    prov_ID = ""
Comments