Hello everybody!
Is it possible to check if an Excel sheet exists after uploading the file using st.file_uploader
?
def my_func():
input_file = st.file_uploader(Upload file", type="xlsx", accept_multiple_files=False)
if input_file:
df1 = pd.read_excel(input_file, sheet_name="sheetName1"
df2 = pd.read_excel(input_file, sheet_name="sheetName2"
sheetName1 might not exist in some instances.
How do I check for its existence?
Thank you so much!
Goyo
2
pandas.ExcelFile
has a sheet_names
attribute.
Awesome, great pointer!
I just canโt figure out now how to trigger the true here:
if input_file:
file = pd.ExcelFile(input_file)
if file.sheet_names == "sheetName1": < this is not triggering it
st.info("Found")
Goyo
4
The docs are not explict and I cannot test right now, but I donโt expect something called sheet_names
to be a string.
Iโll keep testing, and if you can also find something would be appreciated.
Thanks!
Goyo
6
Try printing file.sheet_names
to see how it looks like. And maybe type(file.sheet_names)
to be sure.
I was able to solve this using a for loop.
For those interested:
file = pd.ExcelFile(input_file)
for sheet in file.sheet_names:
if sheet == "sheetName1":
...
if sheet == "sheetName2"
...
Thank you @Goyo for the great suggestion.
Goyo
9
You may want to learn about the in
operator for sequence types.
system
Closed
10
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.