Python: When reading files how to ignore the text between two specific words?

Viewed 84

I am using pandas to read Excel files like this:

My data file in the folder is like:

df = pd.read_excel('Online_Trade_Record_' + str(Number) + '2021-01-19' + '.xlsx')

But apart from the file generated in 2021-01-19, I also have other data files from different dates. I wish to ignore the dates in the code because my Number is unique ID. So just wondering how I can change the code to ignore all the words between str(Number) and '.xlsx'? and only recognize 'Online_Trade_Record_' + Number (whatever in the middle).xlsx

Thanks for help.

1 Answers

I think this will work for your case:

import glob
filename = glob.glob('Online_Trade_Record_' + str(Number) + '*.xlsx')[0]
df = pd.read_excel(filename)
Related