And i am facing the issue in uploading a csv file from local machine through file_uploader and converting it into a pandas data frame using pd.read_csv(). be low is my code.
Please suggest what am i missing here
def csv_test_input(df_train):
""" Function to take File based inputs for Network parameter """
uploaded_file = st.sidebar.file_uploader("Telecom Network Test-Data", type="csv")
if uploaded_file is None:
return None
text_io = io.TextIOWrapper(uploaded_file)
inputDF = pd.read_csv(text_io)
return inputDF
on the first run everythings works fine, but i am getting error on rerun of the app.
Error that i am getting is
ValueError: I/O operation on closed file.
at line text_io = io.TextIOWrapper(uploaded_file)
It looks like you need to reset your buffer with text_io.seek(0) after reading the csv into pandas.
def csv_test_input(df_train):
""" Function to take File based inputs for Network parameter """
uploaded_file = st.sidebar.file_uploader("Telecom Network Test-Data", type="csv")
if uploaded_file is None:
return None
text_io = io.TextIOWrapper(uploaded_file)
inputDF = pd.read_csv(text_io)
text_io.seek(0)
return inputDF
Previously we were creating a new buffer for you each time we reran. To optimize, we are returning the same buffer on rerun. Unfortunately, this means that if you’ve already read the buffer, you’ll need to reset after. If you use .getValue() , there’s no need to seek . Unfortunately for file uploader pandas.read_csv does a read() which will require a buffer reset.
this doesn’t solves my problem, it is still giving me the same error message.
.seek(0) will work if file is open, here the erro is saying that I/O file is closed, i.e. uploaded_file is closed.
can you suggest any workarounds ???
or alternate way to read a csv file into panda frame. I am ready to change my code as well.