0

I'm taking input from the user and then tokenizing it, tokenization is successful but the problem i'm facing is it does not display anything

i am trying to search the words in xlsx file which user inputs and then it should display that complete row in which that specific word exists.

import xlrd
import pandas as pd
from openpyxl import load_workbook
from xlrd import open_workbook
from nltk import word_tokenize


sen = input("Enter your sentence: ")
sent = word_tokenize(sen)
print(sent)


book = open_workbook("Pastho dictionary.xlsx")
for sheet in book.sheets():
    for rowidx in range(sheet.nrows):
        row = sheet.row(rowidx)
        for colidx,cell in enumerate(row):
            for i in sent:
                if cell.value == sent:#row value
                    print ("Found Row Element")
                    print(rowidx, colidx)
                    print(cell.value)
                    print(row)

i expect all the input words to be searched and then display the entire row in which that word exists.

MSFast
  • 13
  • 1
  • 4
  • I would suggest you try => "if sent not in cell.value" instead of "if cell.value == sent" (or if cell.value not in sent) – Lauro Bravar Jan 14 '19 at 08:05
  • 1
    thanks for the help man but it didn't worked – MSFast Jan 14 '19 at 08:38
  • `sent` should expect to be a **list**, how can u compare **list** with **string**? – Lau Real Jan 14 '19 at 08:58
  • Not necesarily. If sent is a string then it can be treated as a list and it checks whether the substring is contained within that string. But maybe I misunderstood the question. (This is what I mean https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – Lauro Bravar Jan 14 '19 at 09:39

1 Answers1

1

Here is how I will go:

import xlwings as xw

bookName = r'first.xlsx'
sheetName = 'Sheet1'

wb = xw.Book(bookName)
sht = wb.sheets[sheetName]

myCell = wb.sheets[sheetName].api.UsedRange.Find('pankaj')

print(myCell.address)

Credit: https://www.vitoshacademy.com/how-to-search-in-a-worksheet-with-python/

quest
  • 3,576
  • 2
  • 16
  • 26