St.file_uploader "AttributeError"

Hello,

I would want to open an .CSV file with st.file_uploader and return a data frame. My code returns a non type object accompanied with an error message of
" AttributeError: ‘NoneType’ object has no attribute ‘iloc’"

Here is the function that I wrote for this task:

data_file = st.sidebar.file_uploader("Upload ELE.CSV" , type=['csv'])
def upload():
    if data_file is not None:
        df= pd.read_csv(data_file , header=None , sep=';' , encoding='unicode_escape' , skip_blank_lines=False)
        return st.dataframe(df)

ELE = upload()
x0 = np.array(ELE.iloc[0 , 1:142].astype(float))
st.write(x0)

at the output I see the content of the file with the error message:

Could anyone make a suggestion to resolve this issue?
Thank you very much.
Payman

Hi @Payman, welcome to the Streamlit community!

Your issue is here. The thing to keep in mind about st.dataframe is that it is a display object, it does not return a dataframe. You should just return df from your function.

Best,
Randy

1 Like

Thank you very much @randyzwitch for your advice.
There is still one problem, I get the same error message before uploading the file. Once it is uploaded, everything works fine. Could you please let me know how to fix this?

Your code doesn’t work because ELE resolves to None for the function upload() on the first run. Then, you try to run np.array on that None object, which won’t work.

I would move the if data_file: statement outside of the upload() function and place it before the ELE creation line.

Best,
Randy

1 Like

Borrowing the idea from here, the code that worked for me is:

st.sidebar.file_uploader( label="Upload ELE.CSV", accept_multiple_files=False, type=['csv', 'xlsx'], key = "Uploaded File" )

if st.session_state["Uploaded File"] is None:
    st.info("Please upload the ELEVATION file in the sidebar")
    st.stop()
elif st.session_state["Uploaded File"] is not None:
    original_file_name  = st.session_state["Uploaded File"].name
    data_file = st.session_state["Uploaded File"]
st.write('File name: ', data_file.name)
ELE = upload(data_file)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.