如何找到具体的文字和打印后,我的下两个字

2024-05-19 09:15:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码在下面。你知道吗

我现在有一个if语句,它找到了一个特定的词,在这个例子中是“配料”。你知道吗

接下来, 而不是print("true")我需要打印“配料”中的下两个单词/字符串。此单词/字符串在图像中出现一次(“成分”)。你知道吗

例如,我运行.py文件,如果我在脚本中包含这个文件,这就是我的输出:print(text)

Ground Almonds

INGREDIENTS: Ground Almonds(100%).

1kg

我只需要重新编码这个部分:

if 'INGREDIENTS' in text:
 print("True")
else:
 print("False")

所以输出是这样的:

INGREDIENTS: Ground Almonds

因为接下来的两个单词/字符串是GroundAlmonds

Python代码

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\Users\gzi\AppData\Roaming\Python\Python37\site-packages\tesseract.exe'

img=Image.open('C:/Users/gzi/Desktop/work/lux.jpg')

text = pytesseract.image_to_string(img, lang = 'eng')


if 'INGREDIENTS' in text:
 print("True")
else:
 print("False")

Tags: 文件字符串代码textintrueif单词
3条回答

因此,假设我们使用pytesseract提取了以下文本:

text = '''Ground Almonds
INGREDIENTS: Ground Almonds(100%).
1kg'''

我们可以通过以下方式实现预期结果:

first_index = text.find('INGREDIENTS')
second_index = text.find('(')
my_string = f'{text[first_index:second_index]}'
print(my_string)

输出为:

INGREDIENTS: Ground Almonds

因此在代码片段中,我们使用find方法来定位INGREDIENTS单词和(符号(假设它总是在主成分之后,指定它的百分比)。你知道吗

然后对上述索引使用string切片并打印结果,用f-string将其格式化为所需的输出。你知道吗

使用正则表达式查找所有匹配项:

import re

txt = "INGREDIENTS: Ground Almonds(\"100\");"
x = re.findall("INGREDIENTS:\s(\w+)\s(\w+)", txt)
print(x)

# [('Ground', 'Almonds')]

如果您不关心百分比并希望避免regex

string = 'INGREDIENTS: Ground Almonds(100%).'

tokens = string.split()
for n,i in enumerate(tokens):
    if 'INGREDIENTS' in i:
        print(' '.join(tokens[n:n+3]))

输出:

INGREDIENTS: Ground Almonds(100%).

相关问题 更多 >

    热门问题