-
Notifications
You must be signed in to change notification settings - Fork 0
/
arffify.py
106 lines (69 loc) · 1.99 KB
/
arffify.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
import sys
import os
import getopt
from datetime import date
import _mysql
import math
def usage():
'''prints the acceptable list of command line options to the user'''
print '''-------------------------------------------------------
Usage
-h or --help print list of commands
-i or --infile input csv file to binify
-o or --outfile output filename to write to
-------------------------------------------------------'''
def main():
pass
try:
options, remainder = getopt.getopt(sys.argv[1:],
'hi:o:',["help","infile=","outfile="])
#fix later
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
infile = None
outfile= None
for opt, arg in options:
if opt in ('-i','--infile'):
infile = arg
elif opt in ('-o','--outfile'):
outfile = arg
elif opt in ('-h','--help'):
usage()
sys.exit(2)
else:
assert False, "unhandled option "+opt
if outfile is None or infile is None:
usage()
print "Error Input and Output files not specified"
sys.exit(2)
inf = open(infile,'r')
indata = inf.readlines()
inf.close()
attribs = indata[0].split(',')
header = '% 1. Title : '+infile.rstrip('.csv')+'\n'
header = header + '''%
%
% 2. Sources:
% (a) Creator : Zach Welch
% (b) Date
@RELATION buggy
'''
exData = indata[1].split(',')
for ndx in range(len(attribs)):
datatype = None
try:
hold = float(exData[ndx])
datatype = 'NUMERIC'
except:
datatype = 'STRING'
header = header + "@ATTRIBUTE "+attribs[ndx].strip()+" "+datatype+'\n'
header = header + "@DATA \n"
outfile= open(outfile,'w')
outfile.write(header)
for line in indata[1:]:
outfile.write(line)
outfile.close()
if __name__ == "__main__":
main()