Simple Word Search in Excel rows with Python

55 Views Asked by At

My code doesn't seem to work properly. I'm trying to look for word_for_find and word_for_find2 in excel rows from B and add to a list corresponding value of the cells from A which contain word_for_find and word_for_find2. Problem is that it doesn't fiind/or maybe it doesn't output all the cells that fulfill these 2 if's. For example this code will only output 4xNumbers from number_list, but in reality there are 6 cells containing both 'zzzzzzzzzzzzzz' and 'alarma'. Funny is that one of the cells contains "alarmas" and not "alarma", but still this one it is in output.

Cells in B contain longer text, not just simple words.

Edit: Forgot to take into consideration capitalization of letters. Any way I can bybass this so it would look for all possible forms of the words? (eg: Alarma, alarma, ALARMA, alarmA, etc)

import openpyxl
from collections import Counter
import string
import xlwt
import xlrd




data = openpyxl.load_workbook('test.xlsx')
sheet = data['ONE1']


i = 0
number_list=[]
word_for_find = 'zzzzzzzzzzzzzz'
word_for_find2 = 'alarma'

for cell in range(1, sheet.max_row + 1):
    s = sheet.cell(row=cell, column=2).value
    if s != None and word_for_find in s:
        if word_for_find2 in s:
            a = sheet.cell(row=cell, column=1).value
            number_list.append(a)
            i += 1
            true = 'yessssss'



print(true)
print('found cells: ',i)
print(number_list)

1

There are 1 best solutions below

0
madacmjtr On

Update : This seems to work

empty = []
for cell in range(1, sheet.max_row + 1):
    s = sheet.cell(row=cell, column=2).value
    empty.append(s)
    for m in range(len(empty)):
        x = empty[m].lower()
    if s != None and word_for_find.lower() in x:
        if word_for_find2.lower() in x:
            a = sheet.cell(row=cell, column=1).value
            asin_list.append(a)
            i += 1
            true = 'yessssss'
    empty.clear()