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

巧了我就是萌 提交于 2019-12-10 03:46:58

问题


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:


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


来源:https://stackoverflow.com/questions/4038703/converting-bibtex-files-to-html-with-python-maybe-pybtex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!