Back to SDS/2 Parametric Scripts

 

### READ FILE DATA

 

### READ FILE DATA

 

''' Input File Data

$

$ SET 10

$

$ hjdsahclaladsalkjls

$PTITLE = SET 10 = SET_110

SET 10 = 1101 1106 1107 1108 1109 1110 1111,

1112 1113 1114 1115 1116 1117 1118,

1119 1120 1121 1122 1123 1124 1125

$ END OF SET 110

$

$

 

SET 11 = 11031 11036 11037 11038 11040 11050 11051,

11052 11053 11054 11055 11056 11057 11058,

$ END OF SET 11

$

$

SET 15 = 110131 110136 110137 110138 110410 110510 110511,

110512 110513 110514 110515 110516 110517 110518,

$ END OF SET 15

$

$

 

SET 19 = 23758 18859 26430 69473 85960 865843 9586 634 3664,

4858 895786 9488 0509 98895,

5488,

4395 9590,

$ END OF SET 19

$

SET 1 = 880 THRU 897 999, 1001, 2001

$HMSET

SET 2 = 921 THRU 932,

     11201,327745,47757,54786598

 

SET 102 = 1001323 THRU 1001331,1001343 THRU 1001349,

        1001359 THRU 1001365,1001375 THRU 1001381,

        1001391 THRU 1001397,1001407 THRU 1001413,

        1001415 THRU 1001429,1001439 THRU 1001445,

        1001455 THRU 1001461,1001471 THRU 1001477,

        1001479 THRU 1001490,1001500 THRU 1001506,

        1001516 THRU 1001522,1001532 THRU 1001538,

        1001540 THRU 1001554,1001564 THRU 1001570,

        1001580 THRU 1001586,1001596 THRU 1001602,

        1001612 THRU 1001618,1001620 THRU 1001634,

        1001644 THRU 1001650,1001660 THRU 1001666,

        1009990,1009992,1009994,1009996,1009998,1010000,1010002,

        1010004,1010006,1010009,1010010,1010012,1010014,

        1010066 THRU 1010081

$HMSET

'''

 

import re

 

def getThruData(s):

    sList = re.findall('\d+ THRU \d+|\d+', s)

    for item in sList:

        if 'THRU' in item:

            tem = item.split(' THRU ')

            for num in range(int(tem[0]), int(tem[1])+1):

                yield num

        else:

            yield int(item.strip())

 

def file_data(s):

    outStr = ''

    in_set = False

    for line in s:

        line = line.replace(',', ' ')

        if line.startswith('SET'):

            in_set = True

            if 'THRU' in line:

                lineList = line.strip().split('=')

                lst = [i for i in getThruData(lineList[1])]

                outStr += '%s=%s ' % (lineList[0], ' '.join([str(i) for i in lst if i != '']))

            else:

                outStr += line.strip('\n,')

        elif (line.startswith('$') or 'END OF SET' in line or line == '\n') and in_set == True:

            in_set = False

            outStr += '\n'

        elif in_set:

            if 'THRU' in line:

                lst = [i for i in getThruData(line)]

                outStr += ' '.join([str(i) for i in lst]) + ' '

            else:

                outStr += ' ' + line.strip('\n,')

    dataDict = {}

    for line in outStr.strip().split('\n'):

        dataDict[line.split('=')[0].strip()] = [int(x) for x in line.split('=')[1].strip().split()]

    return dataDict

 

 

dd = file_data(open('H:/TEMP/temsys/strdata.txt').readlines())

for key in dd:

    print '%s = %s' % (key, dd[key])

 

''' printout:

>>> SET 15 = [110131, 110136, 110137, 110138, 110410, 110510, 110511, 110512, 110513, 110514, 110515, 110516, 110517, 110518]

SET 11 = [11031, 11036, 11037, 11038, 11040, 11050, 11051, 11052, 11053, 11054, 11055, 11056, 11057, 11058]

SET 10 = [1101, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125]

SET 19 = [23758, 18859, 26430, 69473, 85960, 865843, 9586, 634, 3664, 4858, 895786, 9488, 509, 98895, 5488, 4395, 9590]

SET 1 = [880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 999, 1001, 2001]

SET 2 = [921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 11201, 327745, 47757, 54786598]

SET 102 = [1001323, 1001324, 1001325, 1001326, 1001327, 1001328, 1001329, 1001330, 1001331, 1001343, 1001344, 1001345, 1001346, 1001347, 1001348, 1001349, 1001359, 1001360, 1001361, 1001362, 1001363, 1001364, 1001365, 1001375, 1001376, 1001377, 1001378, 1001379, 1001380, 1001381, 1001391, 1001392, 1001393, 1001394, 1001395, 1001396, 1001397, 1001407, 1001408, 1001409, 1001410, 1001411, 1001412, 1001413, 1001415, 1001416, 1001417, 1001418, 1001419, 1001420, 1001421, 1001422, 1001423, 1001424, 1001425, 1001426, 1001427, 1001428, 1001429, 1001439, 1001440, 1001441, 1001442, 1001443, 1001444, 1001445, 1001455, 1001456, 1001457, 1001458, 1001459, 1001460, 1001461, 1001471, 1001472, 1001473, 1001474, 1001475, 1001476, 1001477, 1001479, 1001480, 1001481, 1001482, 1001483, 1001484, 1001485, 1001486, 1001487, 1001488, 1001489, 1001490, 1001500, 1001501, 1001502, 1001503, 1001504, 1001505, 1001506, 1001516, 1001517, 1001518, 1001519, 1001520, 1001521, 1001522, 1001532, 1001533, 1001534, 1001535, 1001536, 1001537, 1001538, 1001540, 1001541, 1001542, 1001543, 1001544, 1001545, 1001546, 1001547, 1001548, 1001549, 1001550, 1001551, 1001552, 1001553, 1001554, 1001564, 1001565, 1001566, 1001567, 1001568, 1001569, 1001570, 1001580, 1001581, 1001582, 1001583, 1001584, 1001585, 1001586, 1001596, 1001597, 1001598, 1001599, 1001600, 1001601, 1001602, 1001612, 1001613, 1001614, 1001615, 1001616, 1001617, 1001618, 1001620, 1001621, 1001622, 1001623, 1001624, 1001625, 1001626, 1001627, 1001628, 1001629, 1001630, 1001631, 1001632, 1001633, 1001634, 1001644, 1001645, 1001646, 1001647, 1001648, 1001649, 1001650, 1001660, 1001661, 1001662, 1001663, 1001664, 1001665, 1001666, 1009990, 1009992, 1009994, 1009996, 1009998, 1010000, 10, 10002, 1010004, 1010006, 1010009, 1010010, 1010012, 1010014, 1010066, 1010067, 1010068, 1010069, 1010070, 1010071, 1010072, 1010073, 1010074, 1010075, 1010076, 1010077, 1010078, 1010079, 1010080, 1010081]

>>>

 

'''

 

 

 

 

'''

# write data to file

f = open("your_file",'w')

for key in sorted(dd):

    f.write('%s %s\n' % (str(key), ''.join(repr(dd[key]).strip('[]').split(','))))

f.close()

'''

 

'''

# write data to file

f = open("H:/TEMP/temsys/strdataOut.txt",'w')       

for key in sorted(dd):

    f.write('%s = %s\n' % (key, dd[key]))

f.close()

'''

''' Above code yields file:

SET 15 = [110131, 110136, 110137, 110138, 110410, 110510, 110511, 110512, 110513, 110514, 110515, 110516, 110517, 110518]

SET 11 = [11031, 11036, 11037, 11038, 11040, 11050, 11051, 11052, 11053, 11054, 11055, 11056, 11057, 11058]

SET 10 = [1101, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125]

SET 19 = [23758, 18859, 26430, 69473, 85960, 865843, 9586, 634, 3664, 4858, 895786, 9488, 509, 98895, 5488, 4395, 9590]

SET 1 = [1, 897]

SET 2 = [1, 932]

'''

 

 

 

 

'''

# write data to file

f = open("H:/TEMP/temsys/strdataOut2.txt",'w')

for key in sorted(dd):

    f.write('%s %s\n' % (key, ''.join(repr(dd[key]).strip('[]').split(','))))

f.close()

'''

''' Above code yields file:

SET 1 1 897

SET 10 1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

SET 11 11031 11036 11037 11038 11040 11050 11051 11052 11053 11054 11055 11056 11057 11058

SET 15 110131 110136 110137 110138 110410 110510 110511 110512 110513 110514 110515 110516 110517 110518

SET 19 23758 18859 26430 69473 85960 865843 9586 634 3664 4858 895786 9488 509 98895 5488 4395 9590

SET 2 1 932

'''

 

 

 

 

'''

dataList = open('H:/TEMP/temsys/strdata.txt').read().split('\n')

 

print data

data = file_data(open('H:/TEMP/temsys/strdata.txt').readlines())

dataDict = {}

for line in data.strip().split('\n'):

    dataDict[line.split('=')[0].strip()] = [int(x) for x in line.split('=')[1].strip().split()] # map(int, line.split('=')[1].strip().split())

for key in dataDict:

    print '%s = %s' % (key, dataDict[key])

 

print

print

data = open('H:/TEMP/temsys/strdata.txt').read()

pat = re.compile("SET \d+ = (\d+.*?)(?:\$ END OF SET| THRU (\d+.*?))",re.M|re.DOTALL)

for result in pat.findall(data):

   print result

'''

'''>>> SET 10 = 1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

SET 11 = 11031 11036 11037 11038 11040 11050 11051 11052 11053 11054 11055 11056 11057 11058

SET 15 = 110131 110136 110137 110138 110410 110510 110511 110512 110513 110514 110515 110516 110517 110518

SET 19 = 23758 18859 26430 69473 85960 865843 9586 634 3664 4858 895786 9488 0509 98895 5488 4395 9590

SET 1 = 1 897

SET 2 = 1 932

SET 15 = [110131, 110136, 110137, 110138, 110410, 110510, 110511, 110512, 110513, 110514, 110515, 110516, 110517, 110518]

SET 11 = [11031, 11036, 11037, 11038, 11040, 11050, 11051, 11052, 11053, 11054, 11055, 11056, 11057, 11058]

SET 10 = [1101, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125]

SET 19 = [23758, 18859, 26430, 69473, 85960, 865843, 9586, 634, 3664, 4858, 895786, 9488, 509, 98895, 5488, 4395, 9590]

SET 1 = [1, 897]

SET 2 = [1, 932]

>>>

>>> sum(dataDict['SET 10'])

23411

>>>

'''

 

### ADDITIONAL CODE

'''

s = 'SET 10 = 1101 1106 1107 1108 1109 1110 1111,\n 1112 1113 1114 1115 1116 1117 1118,\n 1119 1120 1121 1122 1123 1124 1125'

sList = s.split('=')

label = sList[0].strip()

data = sList[1].strip().split(',\n')

datastr = ''.join(data)

 

print '\n%s = %s' % (label, datastr)

dataList = map(int, datastr.split())

print dataList

print

 

>>>

SET 10 = 1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

>>>

'''