-
Notifications
You must be signed in to change notification settings - Fork 5
/
convert_txtx_txt.py
111 lines (83 loc) · 2.8 KB
/
convert_txtx_txt.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
"""
Typical complex rows (extension .txtx as extended txt) are:
1@3 1 2 &if n==1:v=100#else:v=10& 3
or (with the same effect)
1@3 1 2 &if n==1:v=100#if n>1:v=10& 3
or
1@3 &v=100*n& 3
or
1@3 1 2 3
; is quite complicated to be used with if, so we use #
n and v are mandatory names
n is the value in first position of the record
v is the result of the calculation in a formula
& starts and concludes a formula
we can have more than one formula in a row
(but we can have more than a row)
"""
from txtxFunctions import *
fIn = open(project + "/" + fileName + ".txtx", "r")
fOu = open(project + "/" + fileName + ".txt", "w")
nrow = 0
for line in fIn:
nrow += 1
line = fill(line) # to have formulas as blocks without spaces
sline = splitUnfill(line) # split and restores spaces
# print sline
if len(sline) != 0: # skipping empty lines
pos = sline[0].find("@")
# found an @ sign
if pos >= 0:
# the range of the first value
n1 = n2 = 0 # init. required
try:
n1 = int(sline[0][0:pos])
except BaseException:
print(
"no digits or wrong characters on the left of @ in row",
nrow,
"\nexecution stopped in error.")
fIn.close()
fOu.close()
os.sys.exit(1)
# print "*", n1
try:
n2 = int(sline[0][pos + 1:])
except BaseException:
print(
"no digits or wrong characters on the right of @ in row",
nrow,
"\nexecution stopped in error.")
fIn.close()
fOu.close()
os.sys.exit(1)
# print "*", n2
# not found an @ sign
else:
n1 = n2 = int(sline[0])
# output, applying formulas
for n in range(n1, n2 + 1):
# print "%d " % n,
print("%d " % n, end=' ', file=fOu)
for i in range(1, len(sline)):
# check for the presence of a formula
if sline[i].find("=") != -1:
# print "%s " % executeFormula(fIn,fOu,nrow,n,sline[i]),
print(
"%s " %
executeFormula(
fIn,
fOu,
nrow,
n,
sline[i]),
end=' ',
file=fOu)
else:
# print "%s " % sline[i],
print("%s " % sline[i], end=' ', file=fOu)
# print
print(file=fOu)
fIn.close()
fOu.close()
print("File", fileName, "converted: .txtx => .txt\n")