Re: Parsing of Names
"Frédéric Gobry" <[email protected]> Thu, 17 May 2007 21:45:03 +0100
| Newsgroups | gmane.comp.gnome.apps.pybliographer |
|---|---|
| Message-ID | <[email protected]> |
> Below, I have a sample of code that reads a BibTeX file, and breaks the
> data into fields and values. Is there an easy method that is built into
> pybliographer to parse the text of names? I'd like each person's first
> and last name. (And does this code look on track, or am I doing this the
> hard way?)
I might be missing something, but why aren't you using the
BibTeX.Reader class directly? BTW, it has some heuristics to split
author names.
>
> Thanks for any pointers,
>
> -Doug
>
> def parseBibtex(udir, filename, fields):
> """ Pass in directory, filename, and list of valid fields. """
> from Pyblio.Parsers.Syntax.BibTeX.Parser import read, Comment,
> Record, Join, Block, ATComment
> from Pyblio.Exceptions import ParserError
> fd =3D open(udir + "/" + filename, "r")
> try:
> records =3D read(fd)
> except ParserError, err:
> print "ERROR: %s at line %d: %s" % (file, err[0], err[1])
> return
> for rec in records:
> data =3D []
> if type(rec) =3D=3D Comment:
> pass # just a comment
> elif type(rec) =3D=3D ATComment:
> pass
> elif type(rec) =3D=3D Record:
> for pair in rec.fields:
> field, value =3D pair
> field =3D field.strip().lower()
> if type(value) =3D=3D Join:
> for part in value:
> if type(part) =3D=3D Block:
> if field not in fields:
> print p("Unknown field: %s" % field)
> continue
> if field =3D=3D "author" or field =3D=3D "ed=
itor":
> # FIX: break value into names
> else: # other than author
> data.append((field, part.flat()))
> else: # other than block
> data.append((field, part.flat()))
> else: # other than join
> data.append((field, value.flat()))
> else:
> print p("Unknown record type: %s" % type(rec))
> # do something with data
> # return something
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Pybliographer-general mailing list
> Pybliographer-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/pybliographer-general
>
>
-- =
Fr=E9d=E9ric
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/