-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcombine_csv_1.py
67 lines (58 loc) · 1.96 KB
/
combine_csv_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import pandas as pd
columns = ["col1", "col2"]
delimiter = "|"
input_dir = "."
output_dir = ""
csv_flag = 1
headers = None
final_file_name = "a.csv"
data = []
count = 0
for file in os.listdir(input_dir):
# file_path = os.path.join(input_dir, file)
# print(file)
file_path = file
if csv_flag and file_path.endswith(".csv"):
count += 1
print(count, ".", file_path, end=" - ")
try:
if headers is None:
table = pd.read_csv(file_path, header=headers)
table.columns = columns
else:
table = pd.read_csv(file_path)
except UnicodeDecodeError:
if headers is None:
table = pd.read_csv(
file_path, encoding="cp1252", header=headers)
table.columns = columns
else:
table = pd.read_csv(file_path, encoding="cp1252")
table["File"] = file_path
print(table.shape)
data.append(table)
# elif not csv_flag and file_path.endswith(".txt"):
elif not csv_flag and not file_path.endswith(".py"):
count += 1
print(count, ".", file_path, end=" - ")
try:
if headers is None:
table = pd.read_table(
file_path, delimiter=delimiter, header=headers)
table.columns = columns
else:
table = pd.read_table(file_path, delimiter=delimiter)
except UnicodeDecodeError:
if headers is None:
table = pd.read_table(
file_path, delimiter=delimiter, encoding="cp1252", header=headers)
table.columns = columns
else:
table = pd.read_table(
file_path, delimiter=delimiter, encoding="cp1252")
table["File"] = file_path
print(table.shape)
data.append(table)
data = pd.concat(data)
data.to_csv(final_file_name, index=False, encoding="utf8")