0

私は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

私の小さなスクリプト

import gedcom
g = Gedcom('C:\ tmp \ test.ged')//私はWindowsを使用しています
print g.element_list()

ここから、大量の出力「gedcom.Element instance at0x00...」を受け取ります。

この出力を受け取っている理由がわかりません。element_listメソッドに従って、フォーマットされたリストが返されると思いました。私はグーグルでこのサイトを検索しました。答えはおそらく私を正面から見つめていることですが、誰かが明白なことを指摘してくれることを望んでいました。

とても有難い。

4

2 に答える 2

1

someclass instance at 0xdeadbeef__repr__明らかにクラスが定義していないように、クラスを定義していないクラスの標準メソッドの結果であるgedcom.Elementため、問題はそのようなインスタンスのリストを出力することだけにあります。そのようなクラスが定義する場合__str__、あなたは

for x in g.element_list():
    print x

ただし、そうでない場合は、同様の出力が得られます(__str__「デフォルト」として__repr__)。それらの要素、たとえばクラス提供するメソッドでをしたいですか?

于 2010-09-02T02:08:15.790 に答える
0

その出力については、何も悪いことや異常なことはありません。gedcom.Elementが定義されていないため__repr__、リストを印刷するとデフォルトが表示されます__repr__。各要素の特定の属性にアクセスしたい場合は、次のことを試してください。

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

編集:ああ、私はあなたが提供した情報源を調べました。それは確かにを定義しますが、は定義しませ__str____repr__。これがあなたが望むものです、おそらく:

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