Parsing of Names
"Douglas S. Blank" <dblank-8NN4yrnyJ/L2fBVCVOL8/[email protected]> Thu, 17 May 2007 16:33:19 -0400
| Newsgroups | gmane.comp.gnome.apps.pybliographer |
|---|---|
| Organization | Bryn Mawr College |
| Message-ID | <[email protected]> |
Pyblios,
I'm using pybilographer to parse different kinds of files, and then
store the data into a master database available via the web.
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?)
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 = open(udir + "/" + filename, "r")
try:
records = read(fd)
except ParserError, err:
print "ERROR: %s at line %d: %s" % (file, err[0], err[1])
return
for rec in records:
data = []
if type(rec) == Comment:
pass # just a comment
elif type(rec) == ATComment:
pass
elif type(rec) == Record:
for pair in rec.fields:
field, value = pair
field = field.strip().lower()
if type(value) == Join:
for part in value:
if type(part) == Block:
if field not in fields:
print p("Unknown field: %s" % field)
continue
if field == "author" or field == "editor":
# 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/