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

4

37

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?

Pau answered 10/6, 2015 at 22:58 Comment(0)
M
35

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.

Marquetry answered 10/6, 2015 at 23:27 Comment(1)
Note that BibtexParser doesn't actually know how to parse author/editor name lists. I would say pybtex is definitely the way to go.Gluttonize
S
13

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.

Singlehanded answered 16/3, 2018 at 6:53 Comment(4)
Not in my experience. biblib's bib2bib example will not output valid BibTeX for this paper's BibTeXYield
@AlinTomescu you may be using the wrong library. The biblib linked above is not published on PyPI at time of writing. See: github.com/aclements/biblib/issues/4Saprophagous
I see. In the end, I ended up using bibtexparser with more success: bibtexparser.readthedocs.io/en/master/bibtexparser.htmlYield
I tried using @matt's biblib and found that it errored on files that bibtex and biblatex accepted. Ther eis no way to get around parse errors?Egomania
Z
4

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.

Zealous answered 12/7, 2019 at 20:20 Comment(2)
I was able to import without loss by using Bibtex Parser. After that point, any style file, custom definition, or package can be used to write out a string with the bibliography style one wants. I did not test this latter part in detail, however Pybtex seems to be useful here.Zealous
none of those projects is activeXimenes
L
0

The closest thing I know of is the pybtex package

Labret answered 10/6, 2015 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.