import re class Parser: def __init__(self): self.all = [] def parser(self): pattern1 = re.compile(r"m_pData\[\d+\]\t\{x=(.*?) y=(.*?) z=(.*?) \}\tOdGePoint3d\n", re.VERBOSE) pattern2 = re.compile(r"m_pData\[\d+\]\t(.*?)\tdouble\n", re.VERBOSE) pattern3 = re.compile(r"\{x=(.*?) y=(.*?) z=(.*?) \}") end_pattern = re.compile(r"体(\d+)") with open("/home/xiaolong/HeteQuadrature/wxl/data.txt") as f: points = [] bulges = [] extusion = [] for line in f: if line.find("体") != -1: self.all.append([points, bulges, extusion]) points = [] bulges = [] extusion = [] continue match1 = pattern1.findall(line) if match1 != []: points.append(match1) #print(match1) continue match2 = pattern2.findall(line) if match2 != []: bulges.append(match2) #print(match2) continue match3 = pattern3.findall(line) if match3 != []: extusion = match3 #print(match3) continue def generate(self): result = " Loader l;\n\n" result += " std::vector points;\n" result += " std::vector bulges;\n" result += " Vector3D extusion;\n\n" for value in self.all: points, bulges, extusion = value if points == []: continue result += " points.clear();\n" result += " bulges.clear();\n" for point in points: result += " points.push_back(Point3D{" + point[0][0] + "," + point[0][1] + "," + point[0][2] +"});\n" for bulge in bulges: result += " bulges.push_back(" + bulge[0] + ");\n" result += " extusion = Vector3D{" + extusion[0][0] + "," + extusion[0][1] + "," + extusion[0][2] + "};\n" result += " l.addExtrude(points, bulges, extusion);\n\n" print(result) p = Parser() p.parser() # print(p.all) p.generate()