Lashtw commited on
Commit
0c3f391
·
verified ·
1 Parent(s): 6d93cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -204,26 +204,40 @@ def main():
204
 
205
  if uploaded_file:
206
  try:
207
- # 讀取CSV檔案,強制所有欄位為字串以保留前導零
208
  df = pd.read_csv(
209
  StringIO(uploaded_file.getvalue().decode('utf-8')),
210
- header=0, # 假設第一行為標題
211
  dtype=str # 所有欄位都以字串形式讀取
212
  )
213
 
214
- # 篩選並重新命名需要的欄位,忽略「時間戳記」
215
  required_columns = {
216
  "您的姓名": "姓名",
217
- "你的幸運號碼是?(可重複,前面也可以是0。Ex. 0000013、1111111)": "幸運號碼"
 
 
218
  }
219
- df = df[list(required_columns.keys())].rename(columns=required_columns)
220
 
221
  # 檢查是否有必要的欄位
222
- if not all(col in df.columns for col in ["姓名", "幸運號碼"]):
223
- st.error("CSV 檔案缺少 '姓名' '幸運號碼' 欄位,請檢查表單結構。")
224
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
- # 顯示原始資料(僅顯示篩選後的欄位)
227
  st.subheader("原始資料")
228
  st.dataframe(df)
229
 
 
204
 
205
  if uploaded_file:
206
  try:
207
+ # 嘗試讀取 CSV,假設有表頭
208
  df = pd.read_csv(
209
  StringIO(uploaded_file.getvalue().decode('utf-8')),
210
+ header=0, # 預設有表頭
211
  dtype=str # 所有欄位都以字串形式讀取
212
  )
213
 
214
+ # 定義可能的欄位映射
215
  required_columns = {
216
  "您的姓名": "姓名",
217
+ "你的幸運號碼是?(可重複,前面也可以是0。Ex. 0000013、1111111)": "幸運號碼",
218
+ "姓名": "姓名",
219
+ "自選號碼": "幸運號碼"
220
  }
 
221
 
222
  # 檢查是否有必要的欄位
223
+ available_columns = [col for col in required_columns.keys() if col in df.columns]
224
+ if not available_columns or len(available_columns) < 2:
225
+ # 若無表頭或缺少必要欄位,假設無表頭,第一列為姓名,第二列為幸運號碼
226
+ df = pd.read_csv(
227
+ StringIO(uploaded_file.getvalue().decode('utf-8')),
228
+ header=None,
229
+ dtype=str
230
+ )
231
+ if len(df.columns) >= 2:
232
+ df.columns = ["姓名", "幸運號碼"]
233
+ else:
234
+ st.error("CSV 檔案格式錯誤,缺少 '姓名' 或 '幸運號碼' 欄位。")
235
+ return
236
+
237
+ # 篩選並重新命名需要的欄位,忽略「時間戳記」
238
+ df = df[[col for col in df.columns if col in required_columns]].rename(columns=required_columns)
239
 
240
+ # 顯示原始資料
241
  st.subheader("原始資料")
242
  st.dataframe(df)
243