bibtexparser: API

bibtexparser — Parsing and writing BibTeX files

BibTeX <http://en.wikipedia.org/wiki/BibTeX> is a bibliographic data file format.

The bibtexparser module provides parsing and writing of BibTeX files functionality. The API is similar to the json module. The parsed data is returned as a simple BibDatabase object with the main attribute being entries representing bibliographic sources such as books and journal articles.

Parsing is a simple as:

>>>> import bibtexparser
>>>> with open('bibtex.bib') as bibtex_file:
>>>>    bibtex_database = bibtexparser.load(bibtex_file)

And writing:

>>>> import bibtexparser
>>>> with open('bibtex.bib', 'w') as bibtex_file:
>>>>     bibtexparser.dump(bibtex_database, bibtex_file)
bibtexparser.load(bibtex_file, parser=None)[source]

Load BibDatabase object from a file

Parameters:
  • bibtex_file (file) – input file to be parsed
  • parser (BibTexParser) – custom parser to use (optional)
Returns:

bibliographic database object

Return type:

BibDatabase

bibtexparser.loads(bibtex_str, parser=None)[source]

Load BibDatabase object from a string

Parameters:
  • bibtex_str (str or unicode) – input BibTeX string to be parsed
  • parser (BibTexParser) – custom parser to use (optional)
Returns:

bibliographic database object

Return type:

BibDatabase

bibtexparser.dumps(bib_database, writer=None)[source]

Dump BibDatabase object to a BibTeX string

Parameters:
  • bib_database (BibDatabase) – bibliographic database object
  • writer (BibTexWriter) – custom writer to use (optional) (not yet implemented)
Returns:

BibTeX string

Return type:

unicode

bibtexparser.dump(bib_database, bibtex_file, writer=None)[source]

Save BibDatabase object as a BibTeX text file

Parameters:
  • bib_database (BibDatabase) – bibliographic database object
  • bibtex_file (file) – file to write to
  • writer (BibTexWriter) – custom writer to use (optional) (not yet implemented)

bibtexparser.bibdatabase — The bibliographic database object

class bibdatabase.BibDatabase[source]

A bibliographic database object following the data structure of a BibTeX file.

comments = None

List of BibTeX comment (@comment{...}) blocks.

entries = None

List of BibTeX entries, for example @book{...}, @article{...}, etc. Each entry is a simple dict with BibTeX field-value pairs, for example ‘author’: ‘Bird, R.B. and Armstrong, R.C. and Hassager, O.’ Each entry will always have the following dict keys (in addition to other BibTeX fields):

  • ID (BibTeX key)
  • ENTRYTYPE (entry type in lowercase, e.g. book, article etc.)
entries_dict

Return a dictionary of BibTeX entries. The dict key is the BibTeX entry key

preambles = None

List of BibTeX preamble (@preamble{...}) blocks.

strings = None

OrderedDict of BibTeX string definitions (@string{...}). In order of definition.

bibtexparser.bparser — Modifying the default parser

bibtexparser.customization — Record customization functions

bibtexparser.bwriter — Modifying the default writer