# sample points write back
# Another version of reading point data from a file using re
import re
data_file = r'H:\TEMP\temsys\sample_points4.txt'
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()
in_pnt = False
patt
= re.compile(r'''\d+\.\d+E\+\d+| # engineering notation ++
-\d+\.\d+E\+\d+| #
engineering notation -+
-\d+\.\d+E-\d+| #
engineering notation --
\d+\.\d+E-\d+| #
engineering notation +-
\d+\.\d+| # positive float format
-\d+\.\d+| # negative float format
\d+\.| # positive float format
-\d+\.| # negative float format
\d+ # positive integer
''', re.X
)
for line in fList:
lineList
= [x.lower().strip() for x in line.strip().split('
', 1) if x != '']
if in_pnt:
pt_data
+= re.findall(patt, line)
in_pnt =
False
ptDict[convert_data(pt_data[0])] = [convert_data(x)
for x in pt_data[1:] if x != '0']
elif
'pnt' in line.lower() and
'*' in line:
in_pnt =
True
pt_data =
re.findall(patt, line)
elif
'rect' in lineList or 'tria' in lineList:
sLst = re.findall(r'\d{8}',
line)
wireDict[convert_data(sLst[0])] = [convert_data(x) for
x in sLst[1:]]
elif
'pnt' in lineList:
sLst = re.findall(r'\d.{7}|(?<=\d.{7}).+',
line)
ptDict[convert_data(sLst[0])] = [convert_data(x.strip()) for x in sLst[1:]]
return ptDict,wireDict
if __name__ == '__main__':
ptDict,wireDict
= read_file_data(data_file)
new_label =
900001
outList = ['Pnt *%16d %0.8E
%0.8E\n* %0.8E\n' % (new_label+i, item[1][0], item[1][1], item[1][2]) for i, item in enumerate(ptDict.items())]
f = open(r'H:\TEMP\temsys\sample_points4new.txt',
'w')
f.write(''.join(outList))
f.close()
'''
Pnt *
900001 1.36567432E+003 -3.71226532E+002
* 2.01031464E+002
Pnt *
900002 1.15626599E+003 3.13992828E+002
* 1.55018463E+002
Pnt *
900003 1.25433850E+003 -1.42613068E+002
* 1.80202667E+002
Pnt *
900004 1.27057288E+003 -1.75843582E+002
* 1.84236084E+002
Pnt *
900005 1.28286145E+003
-2.01004501E+002
* 1.87218460E+002
Pnt *
900006 1.09715002E+003 2.50676315E+002
* 1.40789337E+002
Pnt *
900007 -1.11547864E+003 -2.71833740E+002
* -1.44698837E+002
Pnt *
900008 1.74560000E-003 -6.99347780E+000
* 1.08377000E-001
'''