fantos commited on
Commit
fbaf608
Β·
verified Β·
1 Parent(s): 691a559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -11
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
- df = pd.read_csv(file.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  dataframes.append(df)
28
 
29
  # λͺ¨λ“  DataFrame 병합
30
- merged_df = pd.concat(dataframes, ignore_index=True)
31
-
32
- # μž„μ‹œ νŒŒμΌμ— μ €μž₯
33
- with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
34
- output_path = tmp.name
35
-
36
- # λ³‘ν•©λœ 데이터 μ €μž₯
37
- merged_df.write_csv(output_path, index=False)
38
-
39
- return output_path, f"{len(files)}개 파일이 μ„±κ³΅μ μœΌλ‘œ λ³‘ν•©λ˜μ—ˆμŠ΅λ‹ˆλ‹€."
 
 
 
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)}"