0

I have a strategic issue of writing a program doing a job.

I have CSV files like:

Column1  Column 2 
-------  ---------- 
parent1 [child1, child2, child3]
parent2 [child4, child5, child6]
child1  [child7, child8]
child5  [child10, child33]
...      ...

It is unknown how deep each element of those lists will be extended and I want to loop through them.

Code:

def make_parentClass(self):
        for i in self.csv_rows_list:
            self.parentClassList.append(parentClass(i))
        # after first Parent    
        for i in self.parentClassList:
            if i.children !=[]:
                for child in i.children:
                    for z in self.parentClassList:
                        if str(child) == str(z.node_parent):
                            i.node_children.append(z)
                            self.parentClassList.remove(z)
class parentClass():
    node_children = []
    def __init__(self, the_list):
        self.node_parent = the_list[0]
        self.children = the_list[1]

The above code might be a solution if I will find a way to iterate. Let me see if you like the question and makes sense now.

Output:

My aim is to build up a treeview through another language but first I need to make this output in JSON format. So the output expected to be something like:

{
  paren1:{'child1':{'child7':{}, 'child8':{}}, 
    'child2': {},
    'child3': {},
  },
  parent2: {
      'child4':{}, 
      'child5': {
          'child10':{},
          'child33':{}
      },
      'child6':{}
  }
}
13
  • 4
    What have you tried so far? Commented Jul 24, 2019 at 12:13
  • The JSON posted in the answer does not correspond with the CSV, did you mean to use [] instead of {} for each child?. Also, to process each item, just use a recursive function once you have your data in the right format. Commented Jul 24, 2019 at 12:15
  • @h4z3 I was mostly thinking about a strategy to solve it. E.g. involving For loops which were not possible. I am also thinking about writing a function that loops back through itself but still can't say much how it looks like. Commented Jul 24, 2019 at 12:17
  • def process_item(self, item): if isinstance(item, list): for i in item: process_item(item) else: do_something_with_item(item) Commented Jul 24, 2019 at 12:23
  • Please provide a minimal reproducible example Commented Jul 24, 2019 at 12:23

1 Answer 1

2

I would recommend a solution using two dictionaries. One nested one with the actually data structure you plan to convert to JSON, and one flat one that will let you actually find the keys. Since everything is a reference in Python, you can make sure that both dictionaries have the exact same values. Carefully modifying the flat dictionary will build your structure for you.

The following code assumes that you have already managed to split each line into a string parent and list children, containing values form the two columns.

json_dict = {}
flat_dict = {}

for parent, children in file_iterator():
    if parent in flat_dict:
        value = flat_dict[parent]
    else:
        value = {}
        flat_dict[parent] = json_dict[parent] = value
    for child in children:
        flat_dict[child] = value[child] = {}

Running this produces json_dict like this:

{
    'parent1': {
        'child1': {
            'child7': {},
            'child8': {}
        },
        'child2': {},
        'child3': {}
    },
    'parent2': {
        'child4': {},
        'child5': {
            'child10': {},
            'child33': {}
        },
        'child6': {}
    }
}

Here is an IDEOne link to play with.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot. It actually seems like a pretty good solution to me. I am going to test it. There is one structure I have to handle, that for me I have each row as ['parent', [child, child, child] but in your solution content is in a Tuple.
It is an elegant answer to me and quite fast but there is a simple issue E.g. if Child1 and its children be input of two parents, its children will appear in only one of the parents. e.g. add this ('parent3', ['child1', 'child2', 'child3'])
That's a completely different question from what you asked. Go ahead and select this answer, then all another with that condition in it.
OK I see. I will. Thanks.
Yes :). Thanks a lot for your help too overall.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.