Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert to a parent child in Python

Posted on 2011-03-21
11
Medium Priority
?
269 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
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
Automating Your MSP Business

The road to profitability.
Delivering superior services is key to ensuring customer satisfaction and the consequent long-term relationships that enable MSPs to lock in predictable, recurring revenue. What's the best way to deliver superior service? One word: automation.

 
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 2000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
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…
In this video tutorial I show you the main steps to install and configure  a VMware ESXi6.0 server. The video has my comments as text on the screen and you can pause anytime when needed. Hope this will be helpful. Verify that your hardware and BIO…
Suggested Courses

971 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