May-05-2022, 01:43 PM
georgebijum Wrote:will you be able to share few lines of code that will handle the issue
The anytree documentation about the «parent» attribute suggests that you can set the attribute after creation, so I suggest something like
nodes = {} with open("SampleHierarchy.csv", "r") as f: # creation step csv_reader = csv.DictReader(f) for row in csv_reader: for item in (row['Code'], row['Parent']): if item and (item not in nodes): nodes[item] = Node(item) # parent setting step f.seek(0) csv_reader = csv.DictReader(f) for row in csv_reader: c, p = row['Code'], row['Parent'] if c and p: nodes[c].parent = nodes[p]