باستخدام محلل Python Gedcom: تلقي إخراج سيئ (مثيل Gedcom.Element على 0x00 ...)

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

  •  26-09-2019
  •  | 
  •  

سؤال

أنا جديد على Python ، ويمكنني أن أقول من الخفافيش تجربة البرمجة الخاصة بي هي اسمية مقارنة بالكثير منكم. استعد أنفسكم :)

لدي ملفان. محلل Gedcom مكتوب في Python وجدته من مستخدم على هذا الموقع (gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html) وملف 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
print g.element_list ()

من هنا ، أتلقى مجموعة من الإخراج "مثيل gedcom.element على 0x00 ..."

لست متأكدًا من سبب تلقي هذا الإخراج. فكرت وفقًا لطريقة element_list ، سيتم إرجاع قائمة منسقة. لقد غوغل وأبحث في هذا الموقع. الجواب ربما يحدق بي في وجهي ولكني كنت آمل أن يتمكن شخص ما من الإشارة إلى ما هو واضح.

مقدر جدا.

هل كانت مفيدة؟

المحلول

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