Link to home
Start Free TrialLog in
Avatar of dmontgom
dmontgom

asked on

Convert to a parent child in Python

Hi,

In Python, I need to convert the below to a parent child table.  How?  Attached is the file.  Below is the sample format

Fields should be parent, child, desc, is_leaf

Thanks


3 /Arts & Entertainment
5 /Computers & Electronics
7 /Finance
8 /Games
11 /Home & Garden
12 /Business & Industrial
13 /Internet & Telecom
14 /People & Society
16 /News
18 /Shopping
19 /Law & Government
20 /Sports
22 /Books & Literature
23 /Arts & Entertainment/Performing Arts
24 /Arts & Entertainment/Visual Art & Design
25 /Business & Industrial/Advertising & Marketing
28 /Business & Industrial/Business Services/Office Services
29 /Real Estate
30 /Computers & Electronics/Hardware
31 /Computers & Electronics/Programming
32 /Computers & Electronics/Software
33 /Arts & Entertainment/Offbeat
34 /Arts & Entertainment/Movies




tax.txt
Avatar of HonorGod
HonorGod
Flag of United States of America image

Well, you're going to have to read the file, and parse it.
Things to note:
Each line has a numeric prefix
? What is the significance of this numeric value?

After the numeric prefix, we have a blank, followed by a delimiter (i.e., '/')
The attached file appears to be well formed.
You should be able to use the string split() method to determine
- numeric prefix
- the number of fields after the prefix.

The data fields should be used to build the "tree"
- Records with smaller numbers of fields appear to be parents

Let's look at an example:
- Line "28" has 3 data fields: <see below>

We expect that this record would be a child of an entry something like:

0000 /Business & Industrial/Business Services

But it doesn't occur in the lines shown above.

However, if we search through the attached file, we find:

329 /Business & Industrial/Business Services

So this would (should) be the parent record.

This tells us that not all parents precede their children in the input file.

So, that should give you a start.

Take a shot, and come back with some questions...
>>> line = '28 /Business & Industrial/Business Services/Office Services'
>>> info = line.split( '/' )
>>> info
['28 ', 'Business & Industrial', 'Business Services', 'Office Services']
>>>

Open in new window

Q: Is the information supposed to be displayed in increasing numeric order, or alphabetically?
Q: Does the tree / table need to be build in a single pass of the data, or are multiple passes allowed?

Note:
  Building the tree / table using a single pass requires a more complex algorithm because "parent" entries may occur after child entries, as described above.
This is more of an interesting problem than I originally expected... ;-)

Is this something like you wanted?

Note:
  Those lines with a numeric value of -1 don't have a complete entry with a value.

I just (now) remembered that you wanted an indicator whether an item "is_leaf"
Please define / describe exactly what determines that.

Thanks
info.py.txt
info.out.txt
Q: How do I display the data in alphabetical order?

A: Use this version of showItems()
#---------------------------------------------------------------------
# Name: showItems()
# Role: Recursively display information about the specified node, and
#       it's child nodes
# Notes:
# - The value of the root node is not displayed
#---------------------------------------------------------------------
def showItems( here, indent ) :
  items = here.getChildren()
  items.sort( lambda x, y : cmp( x.getName(), y.getName() ) )
  for item in items :
    print '%4d %s%s' % ( item.getNumber(), indent, item.getName() )
    showItems( item, indent + '  ' )

Open in new window

Q: How do I display the data in numeric order?

A: Use this version of showItems()
#---------------------------------------------------------------------
# Name: showItems()
# Role: Recursively display information about the specified node, and
#       it's child nodes
# Notes:
# - The value of the root node is not displayed
#---------------------------------------------------------------------
def showItems( here, indent ) :
  items = here.getChildren()
  items.sort( lambda x, y : cmp( x.getNumber(), y.getNumber() ) )
  for item in items :
    print '%4d %s%s' % ( item.getNumber(), indent, item.getName() )
    showItems( item, indent + '  ' )

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dmontgom
dmontgom

ASKER

Hi,

Thanks for the code..I will play with it now....sorry for the delay...was busy....
ok.  I understand completely.

Please let me know if you have questions.

Also, please let me know how important the is_leaf notation is.
If it is important, and needed, please define what it is supposed to mean.

Thanks
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.