uploaded_file = st.file_uploader("選擇一個檔案")
if uploaded_file is not None:
data = pd.read_csv(upload_file)
#Get overview of data: data = pd.read_csv(upload_file)
st.write(data.head())
else:
data = pd.read_excel(upload_file)
#Get overview of data: data = pd.read_excel(upload_file)
st.write(data.head())
如果我上傳excel檔案,它就會給我錯誤,我明白為什么。我必須在讀取csv或excel格式的檔案之前檢查上傳檔案的擴展名。如何檢查上傳檔案的擴展名?
uj5u.com熱心網友回復:
如果我沒有理解錯,有一個簡單的選項。
file_split = uploaded_file.split('. ')
file_extension = '' if len(file_split) ! = 2 else file_split[1]
uj5u.com熱心網友回復:
uploaded_file = st.file_uploader("選擇一個檔案")
#converting dic to str
conv1 = str(upload_file)
#Read file type csv and excel only;(upload_file)
#splitting
split1 = conv1.split(sep=' :', maxsplit=1)
store1 = split1[0]
store2 = store1.split(sep="'"/span>, maxsplit=-1)
store2 = store2[1].split(sep='。', maxsplit=1)
if store2[1]=="csv"/span>:
st.write("ok")
df = pd.read_csv(upload_file)
st.write(df.head())
elif store2[1]=="xlsx"/span>:
st.write("NotOk")
df = pd.read_excel(upload_file)
st.write(df.head())
else:
st.write("Error: upload the file in csv or excel format")
我對ByteIO的概念并不清楚。我試著用這種方式進行了大量的分割。它是有效的,但這并不是有效的方式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/323577.html
標籤:
下一篇:將URL轉換為字串的Regex
