for i, x in enumerate (booster.get_dump(dump_format='text')): pass def txt_c_nodes(tree_string): out="" # Define the pattern for extracting the desired parts pattern = r'(\d+):\[(\w+)([<>=]+)(-?[\d.]+)\]\s+yes=(\d+),no=(\d+),missing=(\d+)' lines=tree_string.replace("\t","").split('\n') for l in lines: if "[" in l and "]" in l: # Use re.findall to extract matching groups matches = re.findall(pattern, l) # Extracted parts if matches: for match in matches: #condition, yes, no, missing = match node,feature, cond, value, yes, no, missing = match index=feature.replace("f","") cond=cond.replace("=","==") out+="node"+node+": if (X ["+index+"] "+cond+value+") goto node"+ yes+" ; else goto node"+no+" ; \n" else: if 'leaf=' in l : #print(l) node=l.split(':leaf=')[0] leaf=l.split(':leaf=')[1] out+='node'+node+': return ' + leaf+" ;\n" return out print(txt_c_nodes(x))