-
Notifications
You must be signed in to change notification settings - Fork 1
/
colour_csv.py
executable file
·125 lines (107 loc) · 2.06 KB
/
colour_csv.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
'''
convert a csv file into a colour coded xls
used for colouring genotypes in a map file
usage: cat inpfile.csv | colour_csv.py outfile.xls A=yellow B=cyan_ega H=green ...
'''
import xlrd,re,xlwt,sys
colours=\
[
'aqua',
'black',
'blue',
'blue_gray',
'bright_green',
'brown',
'coral',
'cyan_ega',
'dark_blue',
'dark_blue_ega',
'dark_green',
'dark_green_ega',
'dark_purple',
'dark_red',
'dark_red_ega',
'dark_teal',
'dark_yellow',
'gold',
'gray_ega',
'gray25',
'gray40',
'gray50',
'gray80',
'green',
'ice_blue',
'indigo',
'ivory',
'lavender',
'light_blue',
'light_green',
'light_orange',
'light_turquoise',
'light_yellow',
'lime',
'magenta_ega',
'ocean_blue',
'olive_ega',
'olive_green',
'orange',
'pale_blue',
'periwinkle',
'pink',
'plum',
'purple_ega',
'red',
'rose',
'sea_green',
'silver_ega',
'sky_blue',
'tan',
'teal',
'teal_ega',
'turquoise',
'violet',
'white',
'yellow',
]
def string2number(value):
'''
try to convert to a float or int
'''
try:
return int(value)
except:
pass
try:
return float(value)
except:
pass
return value
if len(sys.argv) < 2:
for i in colours: print i
print 'usage: cat inpfile.csv | colour_csv.py outfile.xls pat1=col1 pat2=col2...'
exit()
f = sys.stdin
workbook = xlwt.Workbook()
sheetname = 'Sheet1'
sheet = workbook.add_sheet(sheetname)
outfile = sys.argv[1]
symbols = sys.argv[2:]
symdict = {}
for x in symbols:
x = x.split('=')
assert x[1] in colours
symdict[x[0]] = xlwt.easyxf('pattern:pattern solid, fore-colour %s'%x[1])
r=0
for line in f:
tok = line.strip().split(',')
c=0
for x in tok:
value = string2number(x)
if x in symdict:
sheet.write(r,c,value,symdict[x])
else:
sheet.write(r,c,value)
c+=1
r+=1
workbook.save(outfile)