簡體   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