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

2024/11/20 9:41:49

I have a function in python that returns different output of strings (Text). 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. 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. and if True, then exit the function at the point.

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, redef 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

Answer

use regex. The below regex will match given that the string contains 9 adjacent digits.

\d{9,}

https://en.xdnf.cn/q/119851.html

Related Q&A

How to extract quotations from text using NLTK [duplicate]

This question already has answers here:RegEx: Grabbing values between quotation marks(20 answers)Closed 8 years ago.I have a project wherein I need to extract quotations from a huge set of articles . H…

takes exactly 2 arguments (1 given) when including self [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

scipy.optimize.curve_fit a definite integral function with scipy.integrate.quad

If I have a function that the independent variable is the upper limit of an definite integral of a mathematical model. This mathematical model has the parameters I want to do regression. This mathemati…

MAC OS - os.system(command) display nothing

When I run IDLE (python 3.8) :>>> import os >>> os.system("ls") 0 >>> os.system(echo "test") 0 >>> os.system("users") 0 >>> Bu…

Flask App will not load app.py (The file/path provided (app) does not appear to exist)

My flask app is outputting no content for the for() block and i dont know why.I tested my query in app.py , here is app.py:# mysql config app.config[MYSQL_DATABASE_USER] = user app.config[MYSQL_DATABAS…

how to create from month Gtk.Calendar a week calendar and display the notes in each day in python

I have created a calendar app with month and week view in python. In month view, I can write notes in each day, store them in a dictionary and save the dictionary in to disk so I can read it any time.…

How to access inner attribute class from outer class?

As title. the class set a attribute value inside inner class. then, access that inner attribute class from outer function. In below, attribute sets with inner function set_error. then, use outer functi…

Summing up the total based on the random number of inputs of a column

I need to sum up the "value" column amount for each value of col1 of the File1 and export it to an output file. Im new in python and need to do it for thousands of records.File1col1 col2 …

What is wrong with this Binomial Tree Backwards Induction European Call Option Pricing Function?

The function below works perfectly and only needs one thing: Removal of the for loop that creates the 1000 element array arr. Can you help me get rid of that for loop? Code is below #Test with europea…

Regex behaving weird when finding floating point strings [duplicate]

This question already has answers here:re.findall behaves weird(3 answers)Closed 4 years ago.So doing this (in python 3.7.3):>>> from re import findall >>> s = 7.95 + 10 pieces >&g…