简体   繁体   English

Python 在 substring 中查找字符串

[英]Python find string in substring

if substring in string does not works properly. if substring in string不能正常工作。 I get data from Webdriver (Selenium), and all data are strings.我从 Webdriver (Selenium) 获取数据,所有数据都是字符串。 But finding substring from string doesn't work.但是从字符串中找到 substring 是行不通的。 It simply skips my loop without even not entering to it (goes to else statement).它只是跳过了我的循环,甚至没有进入它(转到 else 语句)。

View1_txt = View1_txt.replace('i', 'i').replace('İ', 'I')
View2_txt = View2_txt.replace('i', 'i').replace('İ', 'I')
for s in View1_txt.split():
    if s.isdigit() and len(s) == 4:
        if 1995 <= int(s) <= 2021:
            tp_year = int(s)
            print('year from TP1', tp_year)
            break
#SKIPS THIS LOOP
if car_reg_number_in_application in View1_txt:
    print('number CAR REG matches with TP1')
    car_model_in_reg_list = []
    car_model_in_reg_list.append(car_model_in_reg_form.upper())
    probability = process.extractOne(View1_txt, car_model_in_reg_list)
    if probability[1] >= 57:  # model in TP
        print('model in CAR REG matches with TP1')
        print(probability)
        boolean = check_sub_dict()
        if boolean:
            print('Inside SUB dict - ADD - everything is OK')
            return True  # Decline silently car registration -- Unblock from Schedule Block -- Upload Documents
        else:
            print('Inside MAIN dict')
            main_boolean = check_main_dict()
            if main_boolean:
                print('ADD - everything is OK')
                return True
            else:
                print("MAUNAL - not OK 1")
                return False
    else:
        print("MAUNAL - not OK 2")
        return False


#SKIPS THIS TOO AND GOES TO ELSE
elif car_reg_number_in_application in View2_txt:
    print('number CAR REG matches with TP2')

    car_model_in_reg_list = []
    car_model_in_reg_list.append(car_model_in_reg_form.upper())
    probability = process.extractOne(View1_txt, car_model_in_reg_list)
    if probability[1] >= 57:  # model in TP
        print('model in CAR REG matches with TP2')
        print(probability)
        boolean = check_sub_dict()
        if boolean:
            print('Inside SUB dict - ADD - everything is OK')
            return True  # Decline silently car registration -- Unblock from Schedule Block -- Upload Documents
        elif not boolean:
            print('Inside MAIN dict')
            main_boolean = check_main_dict()
            if main_boolean:
                print('ADD - everything is OK')
                return True
            else:
                print("MAUNAL - not OK 3")
                return False
        else:
            return False
    else:
        print("MAUNAL - not OK 4")
        return False
else:
    print("MAUNAL - not OK 5")
    return False
    #ENTER HERE

I checked all types and everything is a string, tried re search module didn't help.我检查了所有类型,一切都是字符串,尝试re search模块没有帮助。

in which case, the substring is probably not in the string.在这种情况下, substring 可能不在字符串中。

Note that the condition is case sensative:请注意,条件是区分大小写的:

"hello" in "HELLO"
Out[1]: False

"hello".upper() in "HELLO".upper()
Out[2]: True

As it seems to be a car reg, you could also remove non alphanumeric characters:由于它似乎是汽车注册,您还可以删除非字母数字字符:

import re
re.sub(r'\W+', '', car_reg_number_in_application)

Thanks, guys I solved my problem.谢谢各位,我解决了我的问题。 My OCR tool took string O character as 0 integers so why it did not work!我的 OCR 工具将字符串 O 字符作为 0 个整数,所以为什么它不起作用! Thank you a lot十分感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM