forked from phageParser/phageParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderByExpect.py
50 lines (41 loc) · 1.4 KB
/
orderByExpect.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
import time, os, operator, csv
def csv_to_list(csv_file, delimiter=","):
with open (csv_file, 'r') as csv_con:
reader = csv.reader(csv_con, delimiter=delimiter)
return list(reader)
def convert_cells_to_floats(csv_cont):
for row in range(len(csv_cont)):
for cell in range(len(csv_cont[row])):
try:
csv_cont[row][cell] = float(csv_cont[row][cell])
except ValueError:
pass
def sort_by_column(csv_cont, col, reverse=False):
header = csv_cont[0]
body = csv_cont[1:]
if isinstance(col,str):
col_index = header.index(col)
else:
col_index = col
body = sorted(body,
key=operator.itemgetter(col_index),
reverse=reverse)
body.insert(0,header)
return body
def print_csv(csv_content):
print (50*'-')
for row in csv_content:
row=[str(e) for e in row]
print ('\t' .join(row))
print (50*'_')
def write_csv(dest, csv_cont):
with open(dest, 'w') as out_file:
writer= csv.writer(out_file, delimiter=',')
for row in csv_cont:
writer.writerow(row)
for fn in os.listdir("output/"):
csv_cont = csv_to_list('output/'+fn)
convert_cells_to_floats(csv_cont)
csv_sorted = sort_by_column(csv_cont, "Expect")
write_csv("output/sorted/"+"sorted."+fn, csv_sorted)
print_csv(csv_sorted)