PythonのGEDCOMパーサーを使用:悪い出力の受信($ 00でgedcom.Elementインスタンスを...)

StackOverflow https://stackoverflow.com/questions/3623349

  •  26-09-2019
  •  | 
  •  

質問

私は、Pythonに新たなんだ、と私は私のプログラミングの経験があなたの多くに比べ名目でバットをオフに言うことができます。ブレース自分:)

私は2つのファイルを持っています。 A GEDCOMパーサが、私はこのサイト上でユーザーからの(gedcom.pyを見つけたことをPythonで書かれた - <のhref = "http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html" のrel =」 nofollowをnoreferrer "> 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
グラム= GEDCOM( 'C:\ TMP \ test.ged')//私は、Windows
によ 印刷g.element_list()

ここから、私は "0x00のでgedcom.Elementインスタンス..." 出力の束を受け取る

私はこの出力を受けてる理由はわかりません。私は、フォーマットされたリストが返されるelement_list方法に従って思いました。私はGoogleで検索してこのサイトを検索しました。その答えは、おそらく顔で私を見つめているが、私は誰かが明白なことを指摘することができ期待していました。

に感謝。

役に立ちましたか?

解決

someclass instance at 0xdeadbeefは明らかにクラス__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帰属
所属していません StackOverflow
scroll top