Utilizzo di un parser di Python GEDCOM: Ricezione uscita male (esempio gedcom.Element a 0x00 ...)
Domanda
Sono nuovo di Python, e posso dire fuori del blocco la mia esperienza di programmazione è nominale rispetto a molti di voi. Tenetevi forte :)
Ho 2 file. Un GEDCOM parser scritto in Python che ho trovato da un utente su questo sito (gedcom.py - http://ilab.cs.byu.edu/cs460/2006w/assignments/program1.html ) e un semplice file GEDCOM che ho tirato da heiner-eichmann.de/gedcom/gedcom.htm . Indovina chi ha difficoltà a mettere 2 e 2 insieme? Questo ragazzo ...
Ecco un frammento di codice seguito da quello che ho fatto finora.
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
il mio piccolo script
import gedcom
g = Gedcom ( 'C: \ tmp \ test.ged') // Sono su Windows
stampare g.element_list ()
Da qui, ricevo un sacco di uscita "istanza gedcom.Element a 0x00 ..."
Non sono sicuro perché sto ricevendo questa uscita. Ho pensato secondo il metodo element_list un elenco formattato sarebbe tornato. Googled e cercare questo sito. La risposta è probabilmente mi guardava in faccia ma speravo che qualcuno potesse notare l'ovvio.
Molto apprezzato.
Soluzione
someclass instance at 0xdeadbeef
è il risultato del metodo __repr__
standard per le classi che non definiscono uno, come pare classe gedcom.Element
non lo fa, quindi il problema è solo con te la stampa di un elenco di questi casi. Se tali definisce classe __str__
, si potrebbe
for x in g.element_list():
print x
, ma se non lo fa, che darà anche un output simile (come __str__
"default" __repr__
). Cosa vuoi di do con quegli elementi, ad esempio, un metodo che loro classe ha offerta?
Altri suggerimenti
Non c'è niente di sbagliato o di insolito che in uscita. Perché gedcom.Element
non ha definito un __repr__
, stampare l'elenco mostrerà il __repr__
di default. Se si voleva accedere a un attributo particolare su ogni elemento, si potrebbe provare:
print [element.some_attribute for element in g.element_list()]
modifica: Ah, ho guardato oltre la fonte che hai fornito. E in effetti definire una __str__
, ma non __repr__
. Ecco ciò che si vuole, molto probabilmente:
for element in g.element_list()
print element