Python Forum
Nested Data structure question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Data structure question
#1
Hello Everyone,

Newbie to this forum - so, please excuse me if this question has been answered earlier - please redirect me to the right place, if that is the case.

I am trying to parse an excel into a hashmap table - with nested hashes inside. What is the optimal way to do this ?

The structure looks like the following - can be an indeterminate number of rows, and there is no particular pattern to whether a product or attribute is selected or not :

Schema looks like this :
Unique ID | Line Item | Parent Product | Product | Attribute | Value

A given Unique ID can have multiple line items, and each line item can have multiple parent products and so on.

The catch is : for any given line item, a product / attribute entity be present in multiple rows - in one row as a product, in one or more rows as a parent product for some other sub-product or attribute - and this nesting can be anywhere between 1 to 5 levels deep.

I hope I am explaining the question correctly. Is there a way to write a generic dictionary parsing logic to read the data from a file with a few thousand entries or more into a dictionary ?

Thanks in advance for the help and responses.

Arjun
Reply
#2
simplest way to do this is to export the excel to csv, then import directly to a dictionary, using csv.DictReader
If it needs to be totally automated, there are many excel reader programs available see: https://pypi.org/search/?q=excel
Reply
#3
Larsz60+

Many thanks for the response. I do have one question, though :
importing via csv will bring it in as is, but I wouldn't necessarily get a nested hierarchy in the dictionary.

As mentioned earlier, the entities can appear in different columns for the same item - as they can be the child component in one entry, or be the parent entity with other child entities for the same line item.

Will the csv import be able to recognise this and create a proper nested hierarchy in the dictionary.
Reply
#4
It doesn't look like you have a strict hierarchy, it looks like you have a more generalized graph structure. A strict hierarchy you could do in a dictionary, but if it has objects that occur at multiple levels in the hierarchy you are going to need create objects for each entity and link them with edges.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
addition to ichabod's suggestion, perhaps sqlite3?
Reply
#6
Ichabod, Larz60+

Many thanks for your help and clarifications.

From your comments, I am taking away the conclusion that regular excel parsing and storing in dictionary will not help and that I need to write custom code to get the correct hierarchy.
Reply
#7
Yes. It would probably be simpler to read the data in with Larz's suggestion. However, depending on what you want to do with the data once it's read in, my suggestion might end up being easier overall.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Feb-19-2019, 01:41 AM)arjunfen Wrote: I am trying to parse an excel into a hashmap table - with nested hashes inside. What is the optimal way to do this ?
I would try to read data in with Pandas,then try to make sense of data there.
If you can do some manipulation of data in Excel,then all of that and more can also be done in Pandas.
Common Excel Tasks Demonstrated in Pandas.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 3.11 data import question love0715 2 771 Mar-05-2023, 06:50 PM
Last Post: snippsat
  Read nested data from JSON - Getting an error marlonbown 5 1,309 Nov-23-2022, 03:51 PM
Last Post: snippsat
  How can I add certain elements in this 2d data structure and calculate a mean TheOddCircle 3 1,505 May-27-2022, 09:09 AM
Last Post: paul18fr
  Convert nested sample json api data into csv in python shantanu97 3 2,724 May-21-2022, 01:30 PM
Last Post: deanhystad
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,750 Apr-23-2021, 08:09 AM
Last Post: sx999
  what data structure to use? Winfried 4 2,783 Mar-16-2021, 12:11 PM
Last Post: buran
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,558 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  How to use Bunch data structure moish 2 2,830 Dec-24-2020, 06:25 PM
Last Post: deanhystad
  Correct data structure for this problem Wigi 13 4,500 Oct-09-2020, 11:09 AM
Last Post: buran
  difficulties to chage json data structure using json module in python Sibdar 1 2,043 Apr-03-2020, 06:47 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020