Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert to a parent child in Python

Posted on 2011-03-21
11
Medium Priority
?
266 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
Put Machine Learning to Work--Protect Your Clients

Machine learning means Smarter Cybersecurity™ Solutions.
As technology continues to advance, managing and analyzing massive data sets just can’t be accomplished by humans alone. It requires huge amounts of memory and storage, as well as high-speed processing of the cloud.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

#Citrix #XenApp #Citrix Scout #Citrix Insight Services #Microsoft VMMAP #Microsoft ADEXPLORE #Microsoft RAMMAP #Microsoft TCPVIEW #Microsoft AUTORUNS #Microsoft PROCESS EXPLORER #Microsoft PROCESS MONITOR
A look into Log Analysis and Effective Critical Alerting.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

715 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