openfree commited on
Commit
521288b
โ€ข
1 Parent(s): 2011c1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -54,17 +54,41 @@ def read_uploaded_file(file):
54
  content = df.head(10).to_markdown(index=False)
55
  return content, "parquet"
56
  elif file_ext == '.csv':
57
- df = pd.read_csv(file.name)
58
- content = f"๋ฐ์ดํ„ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n{df.head(10).to_markdown(index=False)}\n\n"
59
- content += f"\n๋ฐ์ดํ„ฐ ์ •๋ณด:\n"
60
- content += f"- ์ด ํ–‰ ์ˆ˜: {len(df)}\n"
61
- content += f"- ์ด ์—ด ์ˆ˜: {len(df.columns)}\n"
62
- content += f"- ์ปฌ๋Ÿผ ๋ชฉ๋ก: {', '.join(df.columns)}\n"
63
- return content, "csv"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  else:
65
- with open(file.name, 'r', encoding='utf-8') as f:
66
- content = f.read()
67
- return content, "text"
 
 
 
 
 
 
 
68
  except Exception as e:
69
  return f"ํŒŒ์ผ์„ ์ฝ๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "error"
70
 
 
54
  content = df.head(10).to_markdown(index=False)
55
  return content, "parquet"
56
  elif file_ext == '.csv':
57
+ # CSV ํŒŒ์ผ ์ฝ๊ธฐ ์‹œ ๋‹ค์–‘ํ•œ ์ธ์ฝ”๋”ฉ ์‹œ๋„
58
+ encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
59
+ for encoding in encodings:
60
+ try:
61
+ df = pd.read_csv(file.name, encoding=encoding)
62
+ content = f"๋ฐ์ดํ„ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n{df.head(10).to_markdown(index=False)}\n\n"
63
+ content += f"\n๋ฐ์ดํ„ฐ ์ •๋ณด:\n"
64
+ content += f"- ์ด ํ–‰ ์ˆ˜: {len(df)}\n"
65
+ content += f"- ์ด ์—ด ์ˆ˜: {len(df.columns)}\n"
66
+ content += f"- ์ปฌ๋Ÿผ ๋ชฉ๋ก: {', '.join(df.columns)}\n"
67
+ # ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ •๋ณด ์ถ”๊ฐ€
68
+ content += f"\n์ปฌ๋Ÿผ๋ณ„ ๋ฐ์ดํ„ฐ ํƒ€์ž…:\n"
69
+ for col, dtype in df.dtypes.items():
70
+ content += f"- {col}: {dtype}\n"
71
+ # ๊ฒฐ์ธก์น˜ ์ •๋ณด ์ถ”๊ฐ€
72
+ null_counts = df.isnull().sum()
73
+ if null_counts.any():
74
+ content += f"\n๊ฒฐ์ธก์น˜ ์ •๋ณด:\n"
75
+ for col, null_count in null_counts[null_counts > 0].items():
76
+ content += f"- {col}: {null_count}๊ฐœ\n"
77
+ return content, "csv"
78
+ except UnicodeDecodeError:
79
+ continue
80
+ raise UnicodeDecodeError(f"์ง€์›๋˜๋Š” ์ธ์ฝ”๋”ฉ({', '.join(encodings)})์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
81
  else:
82
+ # ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ธฐ ์‹œ๋„
83
+ encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
84
+ for encoding in encodings:
85
+ try:
86
+ with open(file.name, 'r', encoding=encoding) as f:
87
+ content = f.read()
88
+ return content, "text"
89
+ except UnicodeDecodeError:
90
+ continue
91
+ raise UnicodeDecodeError(f"์ง€์›๋˜๋Š” ์ธ์ฝ”๋”ฉ({', '.join(encodings)})์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
92
  except Exception as e:
93
  return f"ํŒŒ์ผ์„ ์ฝ๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "error"
94