Word search in excel spreadsheet and return of the entire line containing that word in PYTHON

Asked

Viewed 859 times

5

Hi, how are you? I am a beginner in Python and would like to run a script that given a spreadsheet in excel, the user could search a word inside this spreadsheet (input) and if the word was found to return "File Found and display all lines. For example, my spreadsheet: inserir a descrição da imagem aqui

Name Age height Danilo 27 1.62 John 35 1.75 Mary 23 1.85 Thalita 25 1.80

The user seeking Danilo, would like the program to return: File Found and Name Age height Danilo 27 1.62

inserir a descrição da imagem aqui

Follows my code:

from xlrd import open_workbook 
workbook = open_workbook("c:/Python34/test.xlsx")
sheet = workbook.sheet_by_index(0)
prompt = '>'
print ("Entre com o termo a ser buscado: ")
item = input(prompt)
found = False
for cell in sheet.col(0): 
     if cell.value == item:
       found = True
if found == True:
    print('Arquivo Encontrado', item) # tentei colocar (cell) mas dá errado
else:
    print('Arquivo não Encontrado')

Thank you very much

1 answer

1

Particularly I like working with csv more than with xls. Just convert your xls to csv.

You can use regex or findall for this:

import re

data = open('seu_arquivo.csv')

texto = "Bem vindo ao python!"

resultado = re.findall(texto, data.read())

This will return you a list of the things you found without having to loop repetition.

https://docs.python.org/2/library/re.html

  • Hello, thank you so much for the help. I’ll try the suggestion, abç

Browser other questions tagged

You are not signed in. Login or sign up in order to post.