Is there a reliable python library for writing BibTex and outputting it to certain formats?

I am developing Python and Django for a website. I want to take a BibTex record and display it in three different formats: MLA, APA and Chicago. Is there a library that already does this, or will I have to manually format the string?

+9
source share
4 answers

The following projects exist:

If you need comprehensive parsing and output, Pybtex is recommended. An 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.

+11
source

Having tested them, all of these projects are bad for various reasons: terrible APIs, poor documentation and a failure to parse the correct BibTeX files. The implementation you want is not shown in most Google searches, from my own search: it is biblib . This text from README should sell it:

There are many BibTeX parsers. Most of them are complete nonsense, based on some imaginary grammar compiled by the author of the module, which is almost, but not quite, completely different from the actual BibTeX grammar. BibTeX has a grammar. It’s even quite simple, although it’s probably not what you think. The most difficult part of BibTeX grammar is that it is written in only one place: the source code of BibTeX.

+6
source

The closest I know about is the pybtex package

0
source

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

Therefore, Pybtex loses bibtex information when reading and writing a simple .bib file without any changes. Users should be very careful when following the recommendations for using Pybtex.

I will also try biblib and let you know, but the accepted answer should be edited so as not to recommend pybtex.

Edit: I was able to import data using Bibtex Parser, without data loss. However, I had to compile from https://github.com/sciunto-org/python-bibtexparser , since the version installed through pip was wrong at the time. Users must ensure that pip receives the latest version.

As for export, after the data has been imported through BibTex Parser, it is placed in the dictionary and can be exported at the request of the user. BibTex Parser does not have built-in functions for exporting to common formats. Since I do not need this functionality, I have not specifically tested it. However, after importing into the dictionary, the line output can be easily converted to any citation format.

Here Pybtex and a custom style file can help. Instead, I used the stylesheet file provided by the magazine and compiled in LaTeX, but PyBtex has Python style files (but it also allows you to load .sty files). Therefore, I would recommend taking the Bibtex Parser input and moving it to PyBtex (or similar) for output in a specific style.

0
source

All Articles