Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting bibtex files to html with python (maybe pybtex?)

Hi I want to parse a bibtex publications file and sort for specific fields (e.g. year) and filter certain content, to then put it on a website. I came across pybtex, which works as far as reading and parsing the bibtex file, but it is basically not documented and I can't figure out how to sort the entries.

Is pybtex the way to go (how can I sort the entries) or are there better options?

thanks a lot!!

like image 374
gletscher Avatar asked Oct 28 '10 00:10

gletscher


1 Answers

Found a solution, this sorts the entries in a descending order using pybtex, newest publications go first:

from pybtex.database.input import bibtex
from operator import itemgetter, attrgetter
import pprint
parser = bibtex.Parser()
bib_data = parser.parse_file('ref.bib')

def sort_by_year(y, x):
    return int(x[1].fields['year']) - int(y[1].fields['year'])

bib_sorted = sorted(bib_data.entries.items(), cmp=sort_by_year)

for key, value in bib_sorted:
    print key
    print value.fields['year']
    print value.fields['author']
    print value.fields['title']
like image 99
gletscher Avatar answered Sep 17 '22 15:09

gletscher