views:

19

answers:

1

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!!

A: 

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']
gletscher