首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python解析器:接收错误的输出(0x00.中的gedcom.Element实例)

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

Stack Overflow用户
提问于 2010-09-02 02:00:36
回答 2查看 672关注 0票数 0

我是Python新手,我可以毫不夸张地说,与你们中的许多人相比,我的编程经验只是名义上的。(振作起来:)

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

下面是一个代码片段,后面是我迄今为止所做的工作。

代码语言:javascript
复制
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方法,将返回一个格式化的列表。我搜索并搜索了这个网站。答案可能是盯着我的脸,但我希望有人能指出显而易见的。

非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-02 02:08:15

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

代码语言:javascript
复制
for x in g.element_list():
    print x

但如果没有,这也会提供类似的输出(因为__str__“默认为”__repr__)。您想要使用这些元素做什么,例如,它们的类提供的方法提供了什么?

票数 1
EN

Stack Overflow用户

发布于 2010-09-02 02:08:39

这种输出没有什么不对或不寻常的。因为gedcom.Element没有定义__repr__,所以打印列表将显示默认的__repr__。如果希望访问每个元素的特定属性,可以尝试:

代码语言:javascript
复制
print [element.some_attribute for element in g.element_list()]

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

代码语言:javascript
复制
for element in g.element_list()
    print element
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3623349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档