converting bibtex files to html with python (maybe

2019-04-26 19:31发布

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

1条回答
放荡不羁爱自由
2楼-- · 2019-04-26 20:14

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']
查看更多
登录 后发表回答