[英]Streamlit upload CSV file and push it to FTP
我正在嘗試構建一個簡單的 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: expected str, bytes or os.PathLike object, not UploadedFile 的錯誤。
我正在使用 Streamlit v.1.1.0。
請注意,我已經簡化了代碼並替換了 FTP 憑據。 在現實世界中,我可能會使用try/except
會話連接等。
我想你在這里得到錯誤:
file = open(input_file, "rb")
該行既錯誤又無用(您從不使用file
)。 去掉它。
在read_csv
閱讀input_file
后,您可能需要將其找回開頭:
file_input.seek(0)
您缺少storbinary
調用的上傳命令:
session.storbinary("STOR " + input_file.name, input_file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.