代码之家  ›  专栏  ›  技术社区  ›  Florian Vanlangendonck

如何使用st.file_uploader在Streamlit中返回的内容?

  •  0
  • Florian Vanlangendonck  · 技术社区  · 2 年前

    我正在重用一些旧代码,但我试图通过使用Streamlit使其看起来更好。我正在添加一个file_uploader,我想用它上传CSV文件。

    在此之后,我想读取该文件的前11行,以确定如何处理它(我的文件不一定都是相同的)。

    但是,使用Streamlit,file_uploader返回一个类似字节的文件,因此我不能使用“I.readline()”,也不能使用我查看文件开头的代码的下一部分。

    有人能帮我吗?

    谢谢!

    def Generate_dashboard():
        ifile = st.file_uploader("Upload your CSV file here!", type="CSV",accept_multiple_files=False)
        if ifile is not None:
            with open(ifile, 'rb') as i:
                ln = 0header = []
                while ln < 11:
                    header.append(i.readline())
                    ln += 1
                if any("CGTR" in s for s in header):
                    T = 1.e-3
                else:
                    T = 200.e-6
                if header[0].startswith("#Version:"):
                    data = np.genfromtxt(ifile, delimiter=",", skip_header=6, skip_footer=1, names=True) # Skip 6 first lines with header.
                elif header[0].startswith("#discard="):
                    data = np.genfromtxt(ifile, delimiter=",", skip_header=1, skip_footer=1, names=True)
                elif header[0].startswith("beamline id = "):
                    T = 250.e-6data = np.genfromtxt(ifile, delimiter=",", skip_header=10, skip_footer=7, names=True) # Skip 9 first lines with header.
                else:
                    skip = int(input("\nUnknown log format. How many header lines to cut before the channel names? "))
                    data = np.genfromtxt(ifile, delimiter=",", skip_header=skip, skip_footer=1, names=True)
    
    0 回复  |  直到 2 年前