-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv_processing.py
106 lines (83 loc) · 3.22 KB
/
csv_processing.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import csv
def del_col(inputf, outputf, col_num):
"""Delete given column from CSV file and
save remaining columns in a new CSV file
Args:
inputf (str): name of the input CSV file to be used
outputf (str): name of the new CSV file to be created with
one column deleted
col_num (int): column to be deleted from input file (zero-indexed)
"""
with open (inputf) as ifile:
reader = csv.reader(ifile)
first_row = next(reader)
num_cols = len(first_row)
if col_num > num_cols:
print("No such column exists")
return
with open(outputf, "w") as ofile:
writer = csv.writer(ofile, quoting=csv.QUOTE_ALL)
status = []
for row in reader:
if col_num == len(row):
status = row[:col_num-1]
writer.writerow(status)
else:
status = row[:col_num-1] + row[col_num:]
writer.writerow(status)
def sortcol(input_file, out_file, col_num, sort_order = "asc"):
"""Sorts a column of a CSV file in given order, does not sort the entire row.
Args:
input_file (str): name of the input CSV file
out_file (str): name of the output file to be saved
col_num (int): column to be sorted
sort_order (str): "asc" for Ascending or "desc" for Descending
"""
with open(input_file) as ifile:
reader = csv.reader(ifile)
first_row = next(reader)
colnums = len(first_row)
if colnums > len(first_row):
print("No such column exists")
return
sorted_col = []
for row in reader:
col = int(row[col_num-1])
sorted_col.append(col)
sorted_col = sorted(sorted_col)
if sort_order == "desc":
sorted_col.reverse()
with open(out_file, "w") as ofile:
writer = csv.writer(ofile)
for v in sorted_col:
writer.writerow([v])
def full_sort(input_file, out_file, col_num):
"""Sort rows of a CSV file using given column with expanded selection.
Args:
input_file (str): name of CSV file to be sorted
out_file (str): name of the sorted CSV file to be written into
col_num (int): column to be used to sort the input CSV file (zero-indexed)
"""
with open(input_file, 'r') as ifile:
reader = csv.reader(ifile)
first_row = next(reader)
colnums = len(first_row)
if colnums > len(first_row):
print("No such column exists")
return
original_col_list = []
for row in reader:
col = int(row[col_num-1])
original_col_list.append(col)
sorted_col = sorted(original_col_list)
length = len(original_col_list)
index_list = []
rows = [r for r in reader]
for i in range(length):
for j in range(length):
if sorted_col[i] == original_col_list[j]:
index_list.append(j)
with open(out_file, "w") as ofile:
writer = csv.writer(ofile)
for v in index_list:
writer.writerow(rows[v])