0

I have a CSV file in the below format:

Node, Count
A, 10000
A | A1,9000
A | A2, 500
A | A2 | A21, 400
A | A2 | A22, 300
A | A2 | A23, 100
A | A3, 800
A | A3 | A31, 500
A | A3 | A32, 350

I want to build a tree out of this data such that:

A has 3 child nodes - A1, A2, A3
A2 has 3 child nodes - A21,A22,A23
A3 has 2 child nodes - A31, A32

Any Suggestions?

Gaurav Neema
  • 146
  • 1
  • 1
  • 12

1 Answers1

1

You can take the Node column and put it into a dictionary of sets. Dict would have three keys, A, A2, and A3, and the values for those keys would be a set (to avoid having duplicates)

import csv

# dictionary with string keys and set() values
tree = {}

with open('csvfile.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            # skip header
            line_count +=1
        else:
            # grab the first column, split by |, strip whitespace
            nodes = [node.strip() for node in row[0].split('|')]
            for i in range(len(nodes) - 1):
                tree.setdefault(nodes[i], set()).add(nodes[i+1])
            line_count += 1

print(tree)
# output: {'A': {'A3', 'A2', 'A1'}, 'A2': {'A23', 'A22', 'A21'}, 'A3': {'A31', 'A32'}}
Endyd
  • 1,249
  • 6
  • 12
  • This is a nice approach. I was thinking to make tree as it will be easy to traverse a tree later. Anyway, thanks a lot. – Gaurav Neema Jan 09 '19 at 03:55
  • I see. You could take the `tree` dictionary from the code above and insert nodes into a Tree class object that represents a tree, if you have one written. If not, creating a Tree class is a separate question that is well documented here at StackOverflow (https://stackoverflow.com/a/28015122/9462009) – Endyd Jan 09 '19 at 11:45