Home > Blockchain >  Need to know if a value contain 3 letters and 3 numbers
Need to know if a value contain 3 letters and 3 numbers

Time:03-18

I am working with car plate data from my country and I need to check if there is a plate misspelled. The current and correct form is with three letters and three numbers (AAA000), but I saw values like one letter or all numbers. So far I am only capable to know if it is all numbers or letters. Sorry I have no code to share, I'm still too noob.

enter image description here

CodePudding user response:

Supose plate is a list, you can do it with regex:

>>> import re
>>> plates = ['000003', 'TPU553', 'TPU374', 'SVM978']
>>> list(filter(lambda x : re.match(r'\D{3}\d{3}',x),plates))
['TPU553', 'TPU374', 'SVM978']

CodePudding user response:

This function must resolve your problem:

df["Placa"].apply(lambda x: True if x[:3].isalpha() and x[3:].isdigit() and len(x) == 6 else False)

CodePudding user response:

This is my take on your question

def checkcarplate(carplate):
    for i in range(len(carplate)):
        if i<3:
            if not carplate[i].isalpha():
                print('error')
                break
        elif i>2:
            if not carplate[i].isdigit():
                print('error')
                break
            elif i==len(carplate)-1:
                print('ok')    
carplate="A1C123"
checkcarplate(carplate)
carplate="ABC123"
checkcarplate(carplate)
  • Related