Skip to content
Advertisement

Problem with different extension files upload at streamlit

I’m trying to let the user select what files he wants to upload, but I’m facing a problem.

For example, there are two types of extension files the user can upload (csv and xlsx). After he upload his file streamlit needs to open the file and shows as a dataframe. But in code I did, I create two if’s to read the files with xlsx extension and csv extension, but didn’t worked as expected…

Upload file Button:

enter image description here

Here is my code:

import streamlit as st

if option =='Cg':
    st.header("Cálculo do índice de Capabilidade Cg")
    st.text("Irei adicionar algum texto aqui posteriormente para explicar o índice de capabilidade Cdl* e qual equação utilizamoos")
    st.subheader("Faça o Upload do seu Arquivo de Dados abaixo:")
    upload_file = st.file_uploader("Upload Data",type=["csv","xlsx"])

    if upload_file is not None:
        df = pd.read_excel(upload_file)
        st.dataframe(df)
    else:
        df = pd.read_csv(upload_file)
        st.dataframe(df)

Advertisement

Answer

Your if-else statement needs to be modified to:

if upload_file is not None:
    if upload_file.name[0][-4:] == 'xlsx':
        df = pd.read_excel(upload_file)
        st.dataframe(df)
    else:
        df = pd.read_csv(upload_file)
        st.dataframe(df)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement