繁体   English   中英

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

[英]Using a Python GEDCOM parser: Receiving bad output (gedcom.Element instance at 0x00…)

我是Python的新手,可以说,与你们许多人相比,我的编程经验是正常的。 大括号:)

我有2个档案。 我从该站点的用户(gedcom.py- http ://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html)找到了用Python编写的GEDCOM解析器,并提取了一个简单的GEDCOM文件来自heiner-eichmann.de/gedcom/gedcom.htm。 猜猜谁在将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上
打印g.element_list()

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

我不确定为什么会收到此输出。 我认为根据element_list方法将返回格式化的列表。 我已经用Google搜索并搜索了该站点。 答案可能是盯着我,但我希望有人指出这一点。

非常感激。

someclass instance at 0xdeadbeef处的someclass instance at 0xdeadbeef__repr__类的标准__repr__方法的结果,显然是gedcom.Element类没有,因此问题仅在于您打印此类实例的列表。 如果此类定义了__str__ ,则可以

for x in g.element_list():
    print x

但是,如果没有,它也会给出类似的输出(如__str__ “默认为” __repr__ )。 你要什么用这些元素做的 ,例如,他们的阶级确实提供了一种方法?

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

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

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

for element in g.element_list()
    print element

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM