简体   繁体   English

检查字符串是否为九位数,然后在 python 中退出 function

[英]Check if string is of nine digits then exit function in python

I have a function in python that returns different output of strings (Text).我在 python 中有一个 function ,它返回不同的 output 字符串(文本)。 And I have different parts that I should check for the string and if the string is of nine digits or contains 9 digits then I need to perform in the function to exit the function at the point I am newbie at python and I don't know how to exit the function at specific point if a criteria is achieved.我有不同的部分,我应该检查字符串,如果字符串是 9 位数字或包含 9 位数字,那么我需要在 function 中执行以退出 function,此时我是 Z23EEEB4347BDD26BDDFC6B7 的新手,我知道 BDD26BDDFC6B7如果达到标准,如何在特定点退出 function。 For example例如

s = 'oodf 191876320x sd'
print(any(char.isdigit() for char in s))

This checks if the string has digits.这将检查字符串是否有数字。 I need to add another criteria to make sure there are adjacent 9 numbers.我需要添加另一个标准以确保有相邻的 9 个数字。 and if True , then exit the function at the point.如果为True ,则在该点退出 function 。

The code that I am working on is to read number from image (with three different cases of manipulation) This is my try and I welcome any ideas我正在处理的代码是从图像中读取数字(使用三种不同的操作案例)这是我的尝试,我欢迎任何想法

import pytesseract, cv2, re

def readNumber(img):
    img = cv2.imread(img)
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    txt = pytesseract.image_to_string(gry)
    if bool(re.search(r'\d{9}', txt)):
        return re.findall('(\d{9})\D', txt)[0]
    blur = cv2.GaussianBlur(img, (3,3), 0)
    txt = pytesseract.image_to_string(blur)
    if bool(re.search(r'\d{9}', txt)):
        return re.findall('(\d{9})\D', txt)[0]
    thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 51, 4)
    txt = pytesseract.image_to_string(thr, config="digits")
    if bool(re.search(r'\d{9}', txt)):
        return re.findall('(\d{9})\D', txt)[0]
    '''
    
    try:
        txt = pytesseract.image_to_string(gry)
        #txt  = re.findall('(\d{9})\D', txt)[0]
    except:
        thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 51, 4)
        txt = pytesseract.image_to_string(thr, config="digits")
        #txt  = re.findall('(\d{9})\D', txt)[0]

    return txt
'''
    
# M5Pr5         191876320
# RWgrP         202131290
# 6pVH4         193832560
print(readNumber('M5Pr5.png'))
print(readNumber('RWgrP.png'))
print(readNumber('6pVH4.png'))

The related question is on that link Read text below barcode pytesseract python相关问题在该链接上阅读条形码 pytesseract python 下面的文本

You can simply return from the function when the condition is satisfied:当条件满足时,您可以简单地从 function 返回:

# Suppose the string is stored in text variable

if len(text) == 9:
    return

use regex.使用正则表达式。 The below regex will match given that the string contains 9 adjacent digits.鉴于字符串包含 9 个相邻数字,以下正则表达式将匹配。

\d{9,} \d{9,}

to exit a function do you mean to return back from where it was called?退出 function 你的意思是从它被调用的地方返回吗?

if so then use如果是这样,那么使用

if len(stringVarible)==9:
   return

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

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