Solved

Convert to a parent child in Python

Posted on 2011-03-21
11
250 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
  • 8
11 Comments
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
Q: Is the information supposed to be displayed in increasing numeric order, or alphabetically?
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
Revised code, with comments - unsorted output
info.py.txt
0
 

Author Comment

by:dmontgom
Comment Utility
Hi,

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

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now