Package: bibparse
bibparse -- read and write BibTeX filesDescriptionbibparsereads and writes BibTeX files.The main class,Biblio, is adictwith methods for parsing, reading, writing and searching for BibTeX data. Each entry in theBibliois another kind of special dict,BibItem. The user usually only needs to accessBiblio.NOTEJust because I use only{...}myself, I’ve never thought of adding the more traditional"..."field marking before version 1.2.*.Current versionVersion 1.2.0-dev.9 -- 29 July 2021.CopyrightsCopyright © 2019–2021 Legisign.orgmailto:software@legisign.orgThis program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program.  If not, seehttps://www.gnu.org/licenses/.Example usageimport bibparse
db = bibparse.Biblio(filename)
print(db)The sample code loads, parses and pretty-prints a BibTeX file.Module contents1. Helper functionsThe values in some fields (namely author, editor, translator, publisher, address, and pages, all of which may have several names, locations, or other data in their values) ofBibItemare stored internally aslists.to_bibtex(key, val)-- convert an internal Python value into a BibTeX stringto_python(key, val)-- convert a BibTeX string into an internal Python valueBoth functions take a BibTeX field name (key) in order to decide how to handle the value. Except for "pages" where the separator is a single dash "-", the separator is the string " and ", the leading and trailing whitespace included.Examples:to_bibtex('pages', [100, 110])→  '100-110'to_python('address', 'London and New York')→ ['London', 'New York']2. ExceptionsBibError-- the base exceptionDuplicateError-- duplicate ID’sNoIDError-- missing ID in an entryPreambleError-- invalid preambleEach exception has a propertylinenoin order to refer to the line that has raised the exception:try:
    db = bibparse.Biblio(filename)
except bibparse.DuplicateError as exc:
    print(f'duplicate bibid on line {exc.lineno}')3. BibItem classAdict-derived object representing a single BibTeX entry.3.1 MethodsThese are derived fromdictbut modified to ensure lower-case keys, reasonable ordering of keys in a printout, and sort ordering.__lt__()__repr__()__setitem__()parse(data)-- parse string data into a BibTeX entryupdate()-- updateBibliousing another objectparse()can be manually called forstrinput.update()has an additional optionaloverwrite=boolparameter. IfTrue(the default),update()functions exactly likedict.update(), updating BibItem contents from data in the supplieddict. IfFalse, only new keys in supplied data is added but existing values are not overwritten.4. Biblio classThe main class. The constructor can be given an optional filename argument; the file is opened and parsed automatically.4.1  MethodsThe__repr__()method is provided so that merelyprint()ing theBiblioobject produces valid BibTeX output.by_bibid(bibids)-- return all entries whose bibid is in the list (or set)by_regex(field, regex)-- search in field by regexby_types(bibtypes, complement=False)-- search by BibTeX typeparse(data)-- parse string as BibTeX dataread(filename)-- read and parse file as BibTeX datawrite(filename)-- write file in BibTeX formatparse()can be manually called forstrinput; however,read()automatically calls it, as does the constructorBiblio()when given a filename argument.by_bibid(bibids)was written in order to make combining searches easier. Eachby_regex()call returns aBiblioobject whose keys can be obtained withBiblio.keys(). These keys can be used insetoperations to provide a new list of keys that match either any (intersection) or all (union) of the keys, andby_bibid()can then be used to return all the entries.by_regex(field, regex)searches the database by field values and returns the matches in a newBiblioobject. E.g.,by_regex('author', '.*Smith.*')returns all entries where the "author" field contains "Smith".by_types(bibtypes, complement=False)searches the database by BibTeX types (given without the initial"@") and returns the matches in a newBiblioobject.bibtypescan be a string specifying a single type (e.g.,"article") or a list of strings specifying several types (e.g.,["article", "book"]). If the optionalcomplementparameter is set to True, the function returns the complement, i.e., all entriesnotmatching the criteria.