问题描述
您好,我想解析一个bibtex出版物文件并对特定字段(例如年份)进行排序并过滤某些内容,然后将其放到网站上。我遇到了pybtex,它可以在阅读和解析bibtex文件时工作,但它基本上没有记录,我也无法弄清楚如何对条目进行排序。
是pybtex的路要走(我怎么能排序条目)或有更好的选择?
非常感谢!!
<$>解决方案发现一个解决方案,它使用pybtex以递减顺序对条目进行排序,最新的发布先行:
<$ pybtex.database.input中的p $ p>
从运算符导入bibtex
导入itemgetter,attrgetter
导入pprint
解析器= 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 bib_sorted:
打印键
打印value.fields ['year']
打印value.fields ['author']
print value.fields ['title']
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!!
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']
这篇关于使用python将bibtex文件转换为html(也许是pybtex?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!