-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrgbcsv2colordict.py
92 lines (81 loc) · 3.04 KB
/
rgbcsv2colordict.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
#!/usr/bin/env python
# rgbcvs2colorarray.py
# by [email protected]
# Usage: rgbcvs2colorarray.py rgb_combined.csv
# Oputput: A = array([[222,43,221],[2,11,222], ... ])
# Open resulting file rgb_colorarray.py and paste into rgb2colorname.py
from __future__ import print_function
# Begin timer:
import timeit
start_time = timeit.default_timer()
# Get argument list using sys module:
import sys, os.path
def program(*args):
# do whatever
pass
# Provide default file_in name argument if not provided:
if __name__ == "__main__":
#def main(argv):
try:
arg1 = sys.argv[1]
file_in = sys.argv[1]
except IndexError: # getopt.GetoptError:
# print "Usage: " + sys.argv[0] + ' -i <inputfile> -o <outputfile>'
# sys.exit(2)
file_in = 'rgb_combined_v05.csv'
# Exit if file_in not found:
if os.path.exists(file_in) and os.access(file_in, os.R_OK):
import csv, time
with open(file_in, 'rU') as f:
reader = csv.reader(f, delimiter=',')
for i in reader:
header_rows = '# '+time.strftime('%Y-%m-%d %H:%M (local time)')+" "+sys.argv[0]+' START: inrowcount='+ str( sum(1 for _ in f) ) +'.'
print(header_rows)
else:
print('# '+time.strftime('%Y-%m-%d %H:%M (local time)')+' '+sys.argv[0]+" ABORTED. Either file "+file_in+" is missing or is not readable.")
exit(2)
# Provide default file_out name argument if not provided:
if __name__ == "__main__":
#def main(argv):
try:
arg1 = sys.argv[2]
file_out = sys.argv[2]
except IndexError: # getopt.GetoptError:
# Name output file by appending .txt to the name:
file_out = file_in + '.txt'
# Send STDOUT to a file:
stdout = sys.stdout # remember the handle to the real standard output.
sys.stdout=open( file_out,"w")
# Print in yml format:
import csv
# 'rU' means open in universal-newline mode needed on Macs:
#with open('./Portfolio.csv', 'rU') as f:
with open(file_in, 'rU') as f:
reader = csv.reader(f, delimiter=',')
first_line = f.readline() # pull out first line - do not print
print(" HexNameDict={",end="") # no NewLine
# Loop through lines in input to generate: "[222,43,221],[2,11,222]", one for each color:
rownum=0
for i in reader:
strRGBHex=i[0]
if rownum ==0:
# print first row without a comma:
print( '"'+i[0]+'":"'+i[4]+'"',end="")
lastRGBHex=strRGBHex
rownum=rownum+1
else:
if lastRGBHex == strRGBHex:
pass
else:
print(',"'+i[0]+'":"'+i[4]+'"',end="")
lastRGBHex=strRGBHex
rownum=rownum+1
print("}") # end with NewLine
footer_row1="# "+ time.strftime('%Y-%m-%d %H:%M (local time)') +' '+ sys.argv[0] +' '+ file_out +" output "+ str(rownum)+ ' rows.'
print(" "+footer_row1)
# Close the file every time:
sys.stdout.close()
sys.stdout = stdout # Restore regular stdout.
print(footer_row1)
elapsed = timeit.default_timer() - start_time # End timer:
print("# "+ time.strftime('%Y-%m-%d %H:%M (local time)') +' '+ sys.argv[0] +" END: ran for "+ "{:.2f}".format(elapsed * 1000 )+ ' secs.')