【问题标题】:Streamlit upload CSV file, parse it and push it to FTPStreamlit 上传 CSV 文件,解析并推送到 FTP
【发布时间】:2021-12-11 06:14:23
【问题描述】:

我正在尝试构建一个简单的 Streamlit 应用程序,我正在上传一个 CSV 文件,然后将其加载到数据帧中,显示数据帧,然后将其上传到预定义的 FTP 服务器。

第一部分正在工作,文件已成功上传并可视化,但是我无法将其上传到 FTP 服务器。这是我的代码:

import ftplib

import pandas as pd
import streamlit as st

ftp_server = "ftp.test.com"
ftp_username = "user"
ftp_password = "password"

input_file = st.file_uploader("Upload a CSV File",type=['csv'])
if (input_file is not None) and input_file.name.endswith(".csv"):
    df = pd.read_csv(input_file, delimiter="\t", encoding = 'ISO-8859-1')
    st.dataframe(df)
    session = ftplib.FTP(ftp_server, ftp_username, ftp_password)
    file = open(input_file, "rb")
    session.storbinary(input_file.name, input_file)              
    input_file.close()                                    
    session.quit()
    st.success(f"The {input_file.name} was successfully uploaded to the FTP server: {ftp_server}!")

我收到一个错误

TypeError:预期的 str、bytes 或 os.PathLike 对象,而不是 UploadedFile。

我正在使用 Streamlit v.1.1.0。

请注意,我已简化代码并替换了 FTP 凭据。在现实世界中,我可能会使用try/except 进行会话连接等。

【问题讨论】:

    标签: python file-upload ftp ftplib streamlit


    【解决方案1】:
    1. 我猜你在这里得到了错误:

      file = open(input_file, "rb")
      

      那条线既错误又无用(你永远不会使用file)。删除它。

    2. 在read_csv阅读后,您可能需要将input_file重新搜索到开头:

      file_input.seek(0)
      
    3. 您缺少上传命令(STOR)storbinary 调用:

      session.storbinary("STOR " + input_file.name, input_file)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多