Solved

Convert to a parent child in Python

Posted on 2011-03-21
11
262 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:dmontgom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
11 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 35189119
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

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35189954
Q: Is the information supposed to be displayed in increasing numeric order, or alphabetically?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35190043
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:HonorGod
ID: 35192811
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
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35193721
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

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35193733
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

0
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 35193755
Revised code, with comments - unsorted output
info.py.txt
0
 

Author Comment

by:dmontgom
ID: 35246046
Hi,

Thanks for the code..I will play with it now....sorry for the delay...was busy....
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35257125
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
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 36515801
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question