【问题标题】:Python find string in substringPython在子字符串中查找字符串
【发布时间】:2021-08-13 14:47:02
【问题描述】:

if substring in string 无法正常工作。我从 Webdriver (Selenium) 获取数据,所有数据都是字符串。但是从字符串中查找子字符串不起作用。它只是跳过了我的循环,甚至没有进入它(转到 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

我检查了所有类型,一切都是字符串,尝试re search 模块没有帮助。

【问题讨论】:

  • 请解释car_reg_number_in_application
  • 我第二个 @Darina ,但请让我们使用重现您的问题所需的所有代码!所有变量的定义等。

标签: python python-3.x selenium selenium-webdriver substring


【解决方案1】:

在这种情况下,子字符串可能不在字符串中。

注意条件区分大小写:

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

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

由于它似乎是汽车注册,您还可以删除非字母数字字符:

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

【讨论】:

    【解决方案2】:

    谢谢,伙计们,我解决了我的问题。我的 OCR 工具将字符串 O 字符作为 0 个整数,所以为什么它不起作用!非常感谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2014-02-27
      • 2011-07-13
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      相关资源
      最近更新 更多