EmptyDataError: No columns to parse from file about streamlit
Asked Answered
U

4

8

I am using streamlit version v0.68 and currently working on CSV file for data analysis.

st.title('Report Analysis')
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
    data = pd.read_csv(uploaded_file, low_memory=False)
    st.write(data.shape)

First it works, but if I rerun the program in my localhost it gives me the error:

EmptyDataError: No columns to parse from file
Traceback:

File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\streamlit\script_runner.py", line 324, in _run_script
    exec(code, module.__dict__)
File "D:\My Programs\Projects\ReportAnalysis\epl\app.py", line 9, in <module>
    data = pd.read_csv(uploaded_file, low_memory=False)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 686, in read_csv
    return _read(filepath_or_buffer, kwds)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 452, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 946, in __init__
    self._make_engine(self.engine)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 1178, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 2008, in __init__
    self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 540, in pandas._libs.parsers.TextReader.__cinit__

How to handle this error?

Upon answered 14/10, 2020 at 6:23 Comment(6)
what happens when you remove the brackets of head()Assault
If I update anything in the program and rerun it. it shows the same error.Upon
From your error it seems that it is coming from the reading of the file and not the writing of the file. What is "line 9" in your code?Saragossa
data = pd.read_csv(uploaded_file, low_memory=False) this is line 9. I had only written the code and not the imports here.Upon
The error occurs even without st.write(data.head()) when you rerun the scriptTrilogy
If i rerun the code it shows the error.Upon
C
9

According to this post from the Streamlit community page, it is because they are returning the same buffer on the second time the app refreshes. Since pd.read_csv depletes the buffer, the second time you call read_csv will return no rows.

Adding a seek(0) to reset the buffer worked for me.

e.g.

st.title('Report Analysis')
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
    uploaded_file.seek(0)
    data = pd.read_csv(uploaded_file, low_memory=False)
    st.write(data.shape)
Cherub answered 22/10, 2020 at 8:23 Comment(0)
T
2

This problem only occurs with the new version v0.68.1. As a work-around, you can always go back to an older version, e.g. 0.66, using: pip install streamlit=0.66

Trilogy answered 14/10, 2020 at 12:24 Comment(0)
I
1

This will help you. Works with the current version 0.69.1.

global train_upload
train_upload = st.file_uploader("Upload csv data", type=['csv'])
if (train_upload is not None):
    train_features = train_upload.read()
    train_features = str(train_features,'utf-8')
    train_features = StringIO(train_features)
    train_features = pd.read_csv(train_features)
    st.write(train_features)
Introduction answered 15/10, 2020 at 21:9 Comment(0)
B
0

For Streamlit 1.5.0, I tried all of the suggested solutions and none of them worked. The workaround I found was to write the content to a temporary file and then read from it:

uploaded_file = st.file_uploader("Choose a file", type=["csv", "txt"])
content = StringIO(uploaded_file.getvalue().decode("utf-8")).read()
temp_filepath = f"/tmp/{uuid4()}"
with open(temp_filepath, "w") as f:
    f.write(content)
data = read_csv(temp_filepath)
Blondie answered 23/2, 2022 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.