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

发布于 2024-09-17 07:30:25 字数 2020 浏览 7 评论 0原文

我是 Python 新手,我可以立即说,与你们中的许多人相比,我的编程经验是微不足道的。振作起来:)

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

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

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

非常感谢。

I'm new to Python, and I can say off the bat my programming experience is nominal compared to many of you. Brace yourselves :)

I have 2 files. A GEDCOM parser written in Python that I found from a user on this site (gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html) and a simple GEDCOM file that I pulled from heiner-eichmann.de/gedcom/gedcom.htm. Guess who's having trouble putting 2 and 2 together? This guy...

Here is a code snippet followed by what I've done thus far.

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

my little script

import gedcom
g = Gedcom('C:\tmp\test.ged') //I'm on Windows
print g.element_list()

From here, I receive a bunch of output "gedcom.Element instance at 0x00..."

I'm not sure why I'm receiving this output. I thought according to the element_list method a formatted list would be returned. I've Googled and search this site. The answer is probably staring me in the face but I was hoping someone could point out the obvious.

Much Appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-09-24 07:30:25

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

for x in g.element_list():
    print x

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

someclass instance at 0xdeadbeef is the result of the the standard __repr__ method for classes that don't define one, as apparently class gedcom.Element doesn't, so the problem is only with you printing a list of such instances. If such class defines __str__, you could

for x in g.element_list():
    print x

but if it doesn't, that will also give similar output (as __str__ "defaults to" __repr__). What do you want to do with those elements, e.g. a method that their class does offer?

青衫儰鉨ミ守葔 2024-09-24 07:30:25

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

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

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

for element in g.element_list()
    print element

There's nothing wrong or unusual about that output. Because gedcom.Element hasn't defined a __repr__, printing the list will show the default __repr__. If you wanted to access a particular attribute on each element, you could try:

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

edit: Aha, I looked over the source you provided. It does indeed define a __str__, but no __repr__. Here's what you want, most likely:

for element in g.element_list()
    print element
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文