【问题标题】:EmptyDataError: No columns to parse from file about streamlitEmptyDataError:没有要从有关流光的文件中解析的列
【发布时间】:2021-01-28 12:39:41
【问题描述】:

我正在使用 streamlit v0.68 版本,目前正在使用 CSV 文件进行数据分析。

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)

首先它可以工作,但如果我在本地主机中重新运行程序,它会给我错误:

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__

如何处理这个错误?

【问题讨论】:

  • 去掉 head() 的括号会发生什么
  • 如果我更新程序中的任何内容并重新运行它。它显示相同的错误。
  • 从您的错误看来,它似乎来自文件的读取而不是文件的写入。您的代码中的“第 9 行”是什么?
  • data = pd.read_csv(uploaded_file, low_memory=False) 这是第 9 行。这里我只写了代码,没有写导入。
  • 重新运行脚本时即使没有st.write(data.head())也会出现错误

标签: python pandas streamlit


【解决方案1】:

根据this post from the Streamlit community page,这是因为它们在应用程序第二次刷新时返回相同的缓冲区。由于 pd.read_csv 会耗尽缓冲区,因此您第二次调用 read_csv 将不会返回任何行。

添加seek(0) 来重置缓冲区对我有用。

例如

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)

【讨论】:

    【解决方案2】:

    此问题仅在新版本 v0.68.1 中出现。作为一种解决方法,您可以随时返回旧版本,例如0.66,使用: pip install streamlit=0.66

    【讨论】:

      【解决方案3】:

      这将对您有所帮助。适用于当前版本 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)
      

      【讨论】:

        【解决方案4】:

        对于 Streamlit 1.5.0,我尝试了所有建议的解决方案,但都没有奏效。我找到的解决方法是将内容写入临时文件,然后从中读取:

        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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-07-13
          • 2020-03-27
          • 1970-01-01
          • 2020-02-01
          • 2022-11-03
          • 2013-03-09
          • 2011-11-14
          • 2012-06-28
          相关资源
          最近更新 更多