data_file = r'H:\TEMP\temsys\sample_points3.txt'
import re
def convert_data(s):
for func in (int, float):
try:
n = func(s)
return n
except:
pass
return s
def read_file_data(f):
ptDict
= {}
wireDict
= {}
fList
= open(f).readlines()
for line in fList:
lineList
= [x.lower().strip() for x in line.strip().split('
', 1) if x != '']
if 'rect' in lineList or 'tria' in lineList:
wireDict[convert_data(lineList[1][:8])] = \
[convert_data(x)
for x in re.findall(r"\d{8}",
lineList[1])[1:]]
elif
'pnt' in lineList:
ptDict[convert_data(lineList[1][:8])] = \
[convert_data(y.strip())
for y in [x for x in re.split(r"\d{8}", \
lineList[1]) if x !=
''][0].split(' ') if y != '']
return ptDict,wireDict
if __name__ == '__main__':
ptDict,wireDict
= read_file_data(data_file)
print 'Point
dictionary:'
for key in ptDict:
print '%s =
%s' % (key, ptDict[key])
print '\nWire dictionary:'
for key in wireDict:
print '%s =
%s' % (key, wireDict[key])
'''
>>>
Point dictionary:
30400000 = [10.0,
0.0, 0.0]
40000000 = [15.0,
0.0, 0.0]
20020003 = [5.0,
0.0, 0.0]
80009000 = [10.0,
5.0, 0.0]
50050000 = [20.0,
0.0, 0.0]
90009000 = [15.0,
5.0, 0.0]
70000000 = [5.0,
5.0, 0.0]
60000800 = [0.0,
5.0, 0.0]
10010012 = [3.0,
0.0, 0.0]
Wire dictionary:
30000088 =
[30000208, 40000002, 90005000, 80003000]
20000092 =
[20000105, 30000004, 80004000, 71111167]
40000094 =
[40000304, 50000071, 90000600]
10000071 =
[10000101, 20000022, 70000000, 60000055]
>>> '''
''' Data File:
Rect
1000007110000101200000227000000060000055
Rect
2000009220000105300000048000400071111167
Rect
3000008830000208400000029000500080003000
Tria
40000094400003045000007190000600
Pnt
100100123. 0.
0.
Pnt
200200035. 0.
0.
Pnt
3040000010. 0.
0.
Pnt
4000000015. 0.
0.
Pnt
5005000020. 0.
0.
Pnt
600008000. 5.
0.
Pnt
700000005. 5.
0.
Pnt
8000900010. 5.
0.
Pnt
9000900015. 5. 0.
'''
'''
>>> d =
{100: [1,0,0],102: [0,1,0],202: [0,1,1]}
>>> dict(zip([str(x) for x in d.values()], d.keys()))
{'[0, 1, 1]':
202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}
>>>
'''
'''
>>> d =
{100: [1,0,0],102: [0,1,0],202: [0,1,1]}
>>> dict(zip([tuple(x) for x in d.values()], d.keys()))
{(0, 1, 0): 102,
(0, 1, 1): 202, (1, 0, 0): 100}
>>>
'''