XLSX Python Search


With this code I can convert a certain column of an xlsx into a list. I need a function that looks in the directory where I am if there is any file with the values that are in my list called to:

>>> [u'Jose', u'Martin', u'Pedro']
print jose
>>> Jose.xlsx
print martin
>>> Martin.xlsx
print pedro
>>> Pedro.xlsx

As I said before, I need you to look in the folder where I am if there is any file that is named Jose, Martin and Pedro

import csv
import pandas as pd
import xlrd

file_name = 'Today.xlsx'

xl_file = pd.ExcelFile(file_name)

dfs = {sheet_name: xl_file.parse(sheet_name) 
          for sheet_name in xl_file.sheet_names}

a = dfs['Hoja1']['Cliente'] 

a = a.tolist()

jose = a[0]
jose =str(jose) + '{}'.format(file_type)

martin = a[1]
martin =str(martin) + '{}'.format(file_type)

pedro = a[2]
pedro =str(pedro) + '{}'.format(file_type)
asked by Martin Bouhier 21.12.2017 в 02:57

2 answers


This code returns all the files that match the names that are in the list called search.

import sys, os
# lista de diferentes nombres a buscar 
buscar = ["jose", "pedro", "juan"]
# directorio en el cual se ejecuta el script
directorio = os.getcwd()
# lleva la cuenta de la cantidad de archivos
total = 0

for root, dir, ficheros in os.walk(directorio):
    # iteramos en la lista de archivos del directorio
    for fichero in ficheros:
        # iteramos en la lista con los nombres que estamos buscando
        for nombre in buscar:
            # verificamos si de los nombres  que tenemos en la lista coinciden con los nombres de los archivos
            if(nombre in fichero.lower()):
                total += 1

print("En total hay",total," archivos con",buscar)
answered by 21.12.2017 в 03:20

If you only want to search in the directory where you are and not in its subdirectories, discriminate between uppercase and lowercase in the name and only validate a certain extension, you can simply use os.path.isfile .

import os

a = [u'Jose', u'Martin', u'Pedro']
file_type = ".xlsx"

files = {name + file_type for name in a}
existing_files = {file for file in files if os.path.isfile(file)}
nonexistent_files = files - existing_files

If for example there is only Martin.xlsx in your directory the output is:

>>> existing_filesu

>>> nonexistent_files
{u'Pedro.xlsx', u'Jose.xlsx'}
answered by 21.12.2017 в 13:25