0

我是 Python 新手,我可以直截了当地说,与你们中的许多人相比,我的编程经验是名义上的。振作起来:)

我有 2 个文件。我从本网站的用户那里找到的一个用 Python 编写的 GEDCOM 解析器(gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html)和一个我提取的简单 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

我的小剧本

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

从这里,我收到一堆输出“gedcom.Element instance at 0x00 ...”

我不知道为什么我会收到这个输出。我认为根据 element_list 方法会返回一个格式化的列表。我用谷歌搜索并搜索了这个网站。答案可能正盯着我看,但我希望有人能指出显而易见的事情。

非常感激。

4

2 回答 2

1

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

for x in g.element_list():
    print x

但如果没有,那也会给出类似的输出(作为__str__"defaults to" __repr__)。你想对这些元素什么,例如他们的类提供的方法?

于 2010-09-02T02:08:15.790 回答
0

该输出没有任何错误或异常。因为gedcom.Element还没有定义 a __repr__,所以打印列表会显示默认的__repr__。如果您想访问每个元素的特定属性,您可以尝试:

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

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

for element in g.element_list()
    print element
于 2010-09-02T02:08:39.567 回答