import json, re, collections d = json.load(open('ra_shape_raw.json', encoding='utf-8')) BS = chr(92) def cl(t): t = t.strip() if t.startswith(BS + 'f'): t = t.split(';', 1)[-1] return t.strip() T = [(x, y, cl(t), l) for x, y, t, l in d['texts']] tags = [g for g in d['tags'] if g[2].startswith('CSBNUM1')] Ls = [t for t in T if re.match(r'^L\s*=\s*[\d,.]+$', t[2])] Ns = [t for t in T if re.match(r'^N\s*=', t[2])] res = [] for L in sorted(Ls, key=lambda a: (round(a[0] / 6000), -a[1])): x, y = L[0], L[1] n = [t for t in Ns if abs(t[1] - y) < 40 and 0 < t[0] - x < 4000] n = n[0][2] if n else '?' tg = [g for g in tags if abs(g[1] - y) < 450 and 0 < x - g[0] < 4500] tg = sorted(tg, key=lambda g: x - g[0]) at = dict(tg[0][3]) if tg else {} dm = sorted([t for t in T if abs(t[1] - (y - 250)) < 70 and -1300 < t[0] - x < 4500], key=lambda t: t[0]) dm2 = sorted([t for t in T if abs(t[1] - (y - 500)) < 70 and -1300 < t[0] - x < 4500], key=lambda t: t[0]) res.append({'mark': at.get('RB_NO'), 'dia': at.get('RB_DIA'), 'L': L[2], 'N': n, 'dims': [t[2] for t in dm], 'dims2': [t[2] for t in dm2], 'x': round(x), 'y': round(y)}) json.dump(res, open('ra_shapes.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=1) for r in res: print(r['mark'], r['dia'], r['L'], r['N'], r['dims'], r['dims2'], r['x'], r['y'])