Update app.py
Browse files
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 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
else:
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|