简体   繁体   中英

Automatically search folder for specific excel file and import into pandas

I have not seen what I am about to ask anywhere so far.

I have 2 excel files in a folder named say RedRose on say C drive. The files start with say date 09-30-2019_rest_of_name1, ...name2. The _rest_of_name1, ...name2 are static, only dates are updated daily as new files are added into the RedRose folder daily.

Using Python on Run command I want to automatically look in that folder, search for each file name and import each file into its own pandas dataframe.

Thoughts, can this be done with Python?

Not sure where to start

You can get a list of files in the current directory with the glob module.

import glob
files = glob.glob('C:\RedRose\*.xls*')

It returns a list of files with the .xls type of extension and uses regular expressions to check for the right names. Also, the Windows path format might be different

Use the read_excel function in the Pandas library to read the excel files into DataFrames. You can loop through all the file names in files and store each DataFrame as an element of a list or dictionary.

import pandas as pd

dataframes = []
for filename in files:
    dataframes.append(pd.read_excel(filename))

For reading into a dictionary, you need to specify a key for each DataFrame. I would suggest using the filename as the key because it is unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM