Is there a reliable python library for taking a BibTex entry and outputting it into specific formats?

Question:

I’m developing using Python and Django for a website. I want to take a BibTex entry and output it in a view in 3 different formats, MLA, APA, and Chicago. Is there a library out there that already does this or am I going to have to manually do the string formatting?

Asked By: War Gravy

||

Answers:

The closest thing I know of is the pybtex package

Answered By: David Hall

There are the following projects:

If you need complex parsing and output, Pybtex is recommended. Example:

>>> from pybtex.database.input import bibtex
>>> parser = bibtex.Parser()
>>> bib_data = parser.parse_file('examples/foo.bib')
>>> bib_data.entries.keys()
[u'ruckenstein-diffusion', u'viktorov-metodoj', u'test-inbook', u'test-booklet']
>>> print bib_data.entries['ruckenstein-diffusion'].fields['title']
Predicting the Diffusion Coefficient in Supercritical Fluids

Good luck.

Answered By: tatlar

Having tried them, all of these projects are bad, for various reasons: terrible APIs, bad documentation, and a failure to parse valid BibTeX files. The implementation you want doesn’t show up in most Google searches, from my own searching: it’s biblib. This text from the README should sell it:

There are a lot of BibTeX parsers out there. Most of them are complete nonsense based on some imaginary grammar made up by the module’s author that is almost, but not quite, entirely unlike BibTeX’s actual grammar. BibTeX has a grammar. It’s even pretty simple, though it’s probably not what you think it is. The hardest part of BibTeX’s grammar is that it’s only written down in one place: the BibTeX source code.

Answered By: matt

The accepted answer of using pybtex is fraught with danger as Pybtex does not preserve the bibtex format of even simple bibtex files. (https://bitbucket.org/pybtex-devs/pybtex/issues/130/need-to-specially-represent-bibtex-markup)

Pybtex is therefore losing bibtex information when reading and re-writing a simple .bib file without making any changes. Users should be very careful following the recommendations to use pybtex.

I will try biblib as well and report back but the accepted answer should be edited to not recommend pybtex.

Edit:
I was able to import the data using Bibtex Parser, without any loss of data. However, I had to compile from https://github.com/sciunto-org/python-bibtexparser as the version installed via pip was bugged at the time. Users should verify that pip is getting the latest version.

As for exporting, once the data has been imported via BibTex Parser, it’s in a dictionary, and can be exported as the user desires. BibTex Parser does not have built in functions for exporting in common formats. As I did not need this functionality, I didn’t specifically test it. However, once imported into a dictionary, the string output can be converted to any citation format rather easily.

Here, pybtex and a custom style file can help. I used the style file provided by the journal and compiled in LaTeX instead, but PyBtex has python style files (but also allows ingesting .sty files). So I would recommend taking the Bibtex Parser input and transferring it to PyBtex (or similar) for outputting in a certain style.

Answered By: TheDragonReborn
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.