forked from jborowitz/x12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx.py
executable file
·100 lines (91 loc) · 3.23 KB
/
x.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
#!/usr/bin/python3.1
#import urllib2 as url
#import datetime as dt
import csv, os, subprocess, sys, time
from datetime import datetime
import datetime as dt
spc1="series {\n\t title = \""
spc2="\n\tdecimals=3\n\tdata = (\n"
spc3="\t\t)\n\tstart = "
#spc2="\n\tperiod = 12\n\tstart = "
spc4= "\n\t}\nx11{}"
if len(sys.argv) <= 2:
outname = sys.argv[1].split('.')[0] + '-out.csv'
else:
outname = sys.argv[2]
outfiles = {}
times = []
a = csv.DictReader(open(sys.argv[1],'r'))
for i in list(a.fieldnames):
if i != "date":
outfiles[i] = open(i + '.spc','w')
outfiles[i].writelines(spc1 + i + '\"\n')
outfiles[i].writelines(spc2)
for line in a:
date = datetime.strptime(line['date'],'%b-%Y')
# This is where you change the format to parse the time in the .csv file.
# Some examples of characters to put in are below. It is also possible to
# change ouput date formats below using these same codes.
######################################################################
# %b abbreviated month name
# %B full month name
# %d Day of the month as a decimal number [01,31]
# %j Day of the year as a decimal number [001,366]
# %m Month as a decimal number [01,12]
# %y Year without century as a decimal number [00,99]
# %Y Year with century as a decimal number
######################################################################
times.append(date)
for field,v in line.items():
if field != "date":
outfiles[field].write('\t\t' + v + '\n')
times.sort()
startDate = times[0].strftime('%Y.%m')
endDate = times[len(times)-1]
maxyear = endDate.year
print(maxyear)
for filename, file in outfiles.items():
file.writelines(spc3 + startDate + spc4)
#file.writelines('\t\t)\n\tstart = ' + startDate + '\n\tdecimals = 4}\nx11{}\n\n')
file.close()
subprocess.call(['/home/jborowitz/bin/x12a/x12a',filename])
headdict={'date':'date'}
for i in outfiles.keys():
headdict[i + 'sa'] = i + 'sa'
allout = csv.DictWriter(open(outname,'w'),headdict.keys())
allout.writerow( headdict)
outvars = {}
for filestub in outfiles.keys():
print(filestub)
x12out = open(filestub + '.out','r')
line = x12out.readline()
year = -1
outputcsv = open(filestub + '.csv','w')
outputcsv.write('date,' + filestub + '\n')
while line.find('D 11') < 0:
line = x12out.readline()
outvars[filestub]=[]
while year < maxyear:
while not line[2:6].isdigit():
line = x12out.readline()
year = int(line[2:6])
yearout=[]
while len(line) > 10:
st = line[9:64].split()
outvars[filestub].extend(st)
yearout.extend(st)
line = x12out.readline()
for index in range(len(yearout)):
if year < maxyear:
monthDate = dt.date(year,index + 13 - len(yearout),1)
else:
monthDate = dt.date(year,index + 1,1)
dstring = monthDate.strftime('%Y-%m-%d')
outputcsv.write(dstring + ',' + str(yearout[index]) + '\n')
outputcsv.close()
for j in range(len(times)):
row = {}
row['date']=times[j].strftime('%Y-%m-%d')
for i in outfiles.keys():
row[i+'sa']=outvars[i][j]
allout.writerow(row)