使用Python GEDCOM解析器:接收错误输出(在0x00处的gedcom.Element实例...)

时间:2010-09-02 02:00:36

标签: python parsing gedcom

我是Python新手,我可以说,与许多人相比,我的编程经验是名义上的。支持你自己:)

我有2个文件。我从这个站点上的用户(gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html)找到的用Python编写的GEDCOM解析器和一个我从heiner-eichmann.de/gedcom/gedcom.htm中提取的简单GEDCOM文件。猜猜谁将2和2放在一起有困难?这家伙......

这是一个代码片段,后面是我到目前为止所做的事情。

class Gedcom:
""" Gedcom parser

This parser is for the Gedcom 5.5 format.  For documentation of
this format, see

http://homepages.rootsweb.com/~pmcbride/gedcom/55gctoc.htm

This parser reads a GEDCOM file and parses it into a set of
elements.  These elements can be accessed via a list (the order of
the list is the same as the order of the elements in the GEDCOM
file), or a dictionary (the key to the dictionary is a unique
identifier that one element can use to point to another element).

"""

def __init__(self,file):
    """ Initialize a Gedcom parser. You must supply a Gedcom file.
    """
    self.__element_list = []
    self.__element_dict = {}
    self.__element_top = Element(-1,"","TOP","",self.__element_dict)
    self.__current_level = -1
    self.__current_element = self.__element_top
    self.__individuals = 0
    self.__parse(file)

def element_list(self):
    """ Return a list of all the elements in the Gedcom file.  The
    elements are in the same order as they appeared in the file.
    """
    return self.__element_list

def element_dict(self):
    """ Return a dictionary of elements from the Gedcom file.  Only
    elements identified by a pointer are listed in the dictionary.  The
    key for the dictionary is the pointer.
    """
    return self.__element_dict

我的小脚本

导入gedcom
g = Gedcom('C:\ tmp \ test.ged')//我在Windows上 print g.element_list()

从这里,我收到一堆输出“在0x00的gedcom.Element实例......”

我不知道为什么我收到这个输出。我认为根据element_list方法,将返回格式化列表。我用谷歌搜索并搜索这个网站。答案可能是盯着我的脸,但我希望有人可以指出明显的。

非常赞赏。

2 个答案:

答案 0 :(得分:1)

someclass instance at 0xdeadbeef是没有定义一个类的标准__repr__方法的结果,因为显然类gedcom.Element没有,所以问题只出在打印上这种情况的清单。如果此类定义__str__,您可以

for x in g.element_list():
    print x

但如果没有,那也会提供类似的输出(如__str__“默认为”__repr__)。你想用这些元素做什么,例如他们的班级 提供的方法?

答案 1 :(得分:0)

输出没有错误或不寻常。由于gedcom.Element尚未定义__repr__,因此打印列表将显示默认__repr__。如果您想访问每个元素的特定属性,可以尝试:

print [element.some_attribute for element in g.element_list()]

编辑:啊哈,我查看了您提供的来源。它确实定义了__str__,但没有定义__repr__。这就是你想要的,最有可能的:

for element in g.element_list()
    print element