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

1

8

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

Cadmium answered 28/10, 2010 at 0:3 Comment(0)
C
12

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']
Cadmium answered 28/10, 2010 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.