Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,15 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def merge_csv_files(files):
|
| 7 |
"""
|
|
@@ -23,20 +32,38 @@ def merge_csv_files(files):
|
|
| 23 |
# λͺ¨λ νμΌμ DataFrame 리μ€νΈλ‘ μ½κΈ°
|
| 24 |
dataframes = []
|
| 25 |
for file in files:
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
dataframes.append(df)
|
| 28 |
|
| 29 |
# λͺ¨λ DataFrame λ³ν©
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
return None, f"μ€λ₯ λ°μ: {str(e)}"
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
+
import chardet
|
| 6 |
+
|
| 7 |
+
def detect_encoding(file_path):
|
| 8 |
+
"""
|
| 9 |
+
νμΌμ μΈμ½λ©μ κ°μ§νλ ν¨μ
|
| 10 |
+
"""
|
| 11 |
+
with open(file_path, 'rb') as f:
|
| 12 |
+
result = chardet.detect(f.read())
|
| 13 |
+
return result['encoding']
|
| 14 |
|
| 15 |
def merge_csv_files(files):
|
| 16 |
"""
|
|
|
|
| 32 |
# λͺ¨λ νμΌμ DataFrame 리μ€νΈλ‘ μ½κΈ°
|
| 33 |
dataframes = []
|
| 34 |
for file in files:
|
| 35 |
+
# νμΌμ μΈμ½λ© κ°μ§
|
| 36 |
+
encoding = detect_encoding(file.name)
|
| 37 |
+
try:
|
| 38 |
+
df = pd.read_csv(file.name, encoding=encoding)
|
| 39 |
+
except UnicodeDecodeError:
|
| 40 |
+
# κ°μ§λ μΈμ½λ©μ΄ μ€ν¨νλ©΄ λ€λ₯Έ μΈμ½λ© μλ
|
| 41 |
+
encodings_to_try = ['cp949', 'euc-kr', 'latin1', 'ISO-8859-1']
|
| 42 |
+
for enc in encodings_to_try:
|
| 43 |
+
try:
|
| 44 |
+
df = pd.read_csv(file.name, encoding=enc)
|
| 45 |
+
break
|
| 46 |
+
except UnicodeDecodeError:
|
| 47 |
+
continue
|
| 48 |
+
else:
|
| 49 |
+
return None, f"νμΌ '{os.path.basename(file.name)}'μ μΈμ½λ©μ κ²°μ ν μ μμ΅λλ€."
|
| 50 |
+
|
| 51 |
dataframes.append(df)
|
| 52 |
|
| 53 |
# λͺ¨λ DataFrame λ³ν©
|
| 54 |
+
if dataframes:
|
| 55 |
+
merged_df = pd.concat(dataframes, ignore_index=True)
|
| 56 |
+
|
| 57 |
+
# μμ νμΌμ μ μ₯
|
| 58 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
|
| 59 |
+
output_path = tmp.name
|
| 60 |
+
|
| 61 |
+
# λ³ν©λ λ°μ΄ν° μ μ₯
|
| 62 |
+
merged_df.to_csv(output_path, index=False, encoding='utf-8')
|
| 63 |
+
|
| 64 |
+
return output_path, f"{len(files)}κ° νμΌμ΄ μ±κ³΅μ μΌλ‘ λ³ν©λμμ΅λλ€."
|
| 65 |
+
else:
|
| 66 |
+
return None, "λ³ν©ν λ°μ΄ν°κ° μμ΅λλ€."
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
return None, f"μ€λ₯ λ°μ: {str(e)}"
|