El uso de un analizador de Python GEDCOM: Recepción de malas salida (ejemplo gedcom.Element en 0x00 ...)
Pregunta
Soy nuevo en Python, y puedo decir de buenas a mi experiencia en programación es nominal en comparación con muchos de ustedes. Prepárense :)
Tengo 2 archivos. A GEDCOM analizador escrito en Python que he encontrado de un usuario en este sitio (gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html ) y un archivo GEDCOM simple que me sacó de heiner-eichmann.de/gedcom/gedcom.htm . Adivinar quién está teniendo problemas para poner 2 y 2 juntos? Este tipo ...
Aquí está un fragmento de código seguido de lo que he hecho hasta ahora.
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
mi pequeño script
importación GEDCOM
g = Gedcom ( 'C: \ tmp \ test.ged') // Estoy en Windows
imprimir g.element_list ()
A partir de aquí, recibo un montón de salida "instancia gedcom.Element en 0x00 ..."
No estoy seguro de por qué estoy recibiendo esta salida. Pensé según el método element_list sería devuelto una lista formateada. He buscado en Google y buscar en este sitio. La respuesta es, probablemente, me mirando a la cara, pero yo estaba esperando que alguien podría señalar lo obvio.
muy apreciada.
Solución
someclass instance at 0xdeadbeef
es el resultado del método __repr__
estándar para las clases que no definen una, ya que aparentemente gedcom.Element
clase no lo hace, por lo que el problema es sólo con usted la impresión de una lista de tales casos. Si tales define la clase __str__
, usted podría
for x in g.element_list():
print x
pero si no es así, que también dará un resultado similar (como __str__
"por defecto" __repr__
). ¿Qué desea hacer con esos elementos, por ejemplo, un método que su clase hace oferta?
Otros consejos
No hay nada malo o inusual en esa salida. Debido gedcom.Element
no ha definido una __repr__
, imprimir la lista mostrará el __repr__
defecto. Si usted quiere acceder a un atributo particular en cada elemento, podría intentar:
print [element.some_attribute for element in g.element_list()]
editar Aha, me daba la fuente que proporcionó. Sí define de hecho un __str__
, pero sin __repr__
. Esto es lo que quiere, lo más probable:
for element in g.element_list()
print element