file_content_viewer.py
· 894 B · Python
原始檔案
import streamlit as st
import os
# 標題
st.title("文件內容查看器")
# 文件上傳
uploaded_file = st.file_uploader("請上傳文字或 Markdown 文件", type=["txt", "md"])
# 如果有文件被上傳
if uploaded_file is not None:
# 讀取文件內容
content = uploaded_file.read().decode("utf-8")
# 儲存文件內容到暫存檔
temp_file = f"temp_{uploaded_file.name}"
with open(temp_file, "w", encoding="utf-8") as f:
f.write(content)
# 顯示文件內容
st.subheader(f"文件內容 ({uploaded_file.name})")
st.write(content)
# 後續操作
st.write("你可以在這裡對文件內容進行後續操作…")
# 範例: 讀取暫存檔內容
with open(temp_file, "r", encoding="utf-8") as f:
temp_content = f.read()
st.write(f"暫存檔內容: {temp_content}")
# 刪除暫存檔
os.remove(temp_file)
| 1 | import streamlit as st |
| 2 | import os |
| 3 | |
| 4 | # 標題 |
| 5 | st.title("文件內容查看器") |
| 6 | |
| 7 | # 文件上傳 |
| 8 | uploaded_file = st.file_uploader("請上傳文字或 Markdown 文件", type=["txt", "md"]) |
| 9 | |
| 10 | # 如果有文件被上傳 |
| 11 | if uploaded_file is not None: |
| 12 | # 讀取文件內容 |
| 13 | content = uploaded_file.read().decode("utf-8") |
| 14 | |
| 15 | # 儲存文件內容到暫存檔 |
| 16 | temp_file = f"temp_{uploaded_file.name}" |
| 17 | with open(temp_file, "w", encoding="utf-8") as f: |
| 18 | f.write(content) |
| 19 | |
| 20 | # 顯示文件內容 |
| 21 | st.subheader(f"文件內容 ({uploaded_file.name})") |
| 22 | st.write(content) |
| 23 | |
| 24 | # 後續操作 |
| 25 | st.write("你可以在這裡對文件內容進行後續操作…") |
| 26 | # 範例: 讀取暫存檔內容 |
| 27 | with open(temp_file, "r", encoding="utf-8") as f: |
| 28 | temp_content = f.read() |
| 29 | st.write(f"暫存檔內容: {temp_content}") |
| 30 | |
| 31 | # 刪除暫存檔 |
| 32 | os.remove(temp_file) |