joeyaurel / python-gedcom

Python module for parsing, analyzing, and manipulating GEDCOM files
https://gedcom.joeyaurel.dev
GNU General Public License v2.0
154 stars 39 forks source link

trying to separate gedcom into maternal and paternal, error #20

Closed codelocksdev closed 5 years ago

codelocksdev commented 5 years ago

Hello, I'm trying to separate out the maternal vs paternal lines in my gedcom. Here's the code as I have it now:

gedcom = Gedcom(file_path)
all_records = gedcom.get_root_child_elements()
home_person = gedcom.get_root_element()
parents = gedcom.get_parents(home_person)

and I get the following output:

Traceback (most recent call last):
  File "C:\Users\saman\Documents\ancestry\python-gedom-playground\bridgets.py", line 15, in <module>
    parents = gedcom.get_parents(home_person)
  File "C:\Python37\lib\site-packages\gedcom\__init__.py", line 456, in get_parents
    raise ValueError("Operation only valid for elements with %s tag." % GEDCOM_TAG_INDIVIDUAL)
ValueError: Operation only valid for elements with INDI tag.

What am I doing wrong?

damonbrodie commented 5 years ago

The root element is not a person - its a special element that "anchors" the dictionary. This is done because all the elements become a "child" to the "parent" element.

In your case you want to iterate through your "all_records" and look for individuals:

all_records = gedcom.get_root_child_elements()
for record in all_records:
    if record.is_individual():
        parents = gedcom.get_parents(record)
            for parent in parents:
                print (parent.get_name())
codelocksdev commented 5 years ago

Thank you.

joeyaurel commented 5 years ago

This behavior will become more clear with version 1.0.0 @sstephans :) There will be derived Element classes like RootElement and IndividualElement.

Thank you! :)

codelocksdev commented 5 years ago

That's exciting! I've been having a lot of fun with this repo, using it to make custom google maps of ancestors. Thanks for your work!