-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_data_for_excel.py
92 lines (78 loc) · 1.99 KB
/
generate_data_for_excel.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/python2.7
import sys, getopt
def generate_data_for_excel(parafile, datafile, mode, outputfile):
paras = []
try:
with open(parafile, 'r') as f:
for line in f.readlines():
line = line[:len(line)-1]
paras.append(line)
f.close()
#print paras
except Exception, e:
print e
data_count = 0
data_lines = []
try:
lines = 0
with open(datafile, 'r') as f:
for line in f.readlines():
line = line[:len(line)-1]
data_lines.append(line)
lines += 1
f.close()
data_count = lines / len(paras)
print "total para : lines = %d" % len(paras)
print "total data : lines = %d" % lines
except Exception, e:
print e
try:
with open(outputfile, 'w') as f:
for index in range(len(paras)) :
outstr = ""
outstr += paras[index]
outstr += "|"
if mode == "inc" :
c = 1
while (c < data_count) :
outstr += (str(int(data_lines[index + c * len(paras)]) \
- int(data_lines[index + (c-1) * len(paras)]))+ "|")
c += 1
else :
for c in range(data_count) :
outstr += ((data_lines[index + c * len(paras)]) + "|")
outstr += "\n"
#print outstr
f.write(outstr)
f.close()
except Exception, e:
print e
def parse_arg(argv):
parafile = ''
datafile = ''
mode = ''
try:
opts, args = getopt.getopt(argv,"hp:d:m:",["para=","data=","mode="])
except getopt.GetoptError:
print 'test.py -p <para> -o <data>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -p <para> -d <data> -m <inc>'
sys.exit()
elif opt in ("-p", "--para"):
parafile = arg
elif opt in ("-d", "--data"):
datafile = arg
elif opt in ("-m", "--mode"):
mode = arg
return parafile, datafile, mode
if __name__ == "__main__":
parafile = ''
datafile = ''
parafile, datafile, mode = parse_arg(sys.argv[1:])
#print 'para file is ', parafile
#print 'data file is ', datafile
if parse_arg == '' or datafile == '':
print sys.argv[0] + ' -p <para> -d <data>'
generate_data_for_excel(parafile, datafile, mode, "output.txt")