Pybtex does not recogonize bibtex entry

大憨熊 提交于 2019-12-12 18:29:08

问题


I'm creating a publications database which allows users to enter bibtex entries which I then parse and store in a db. Right now, I'm having trouble parsing the bibtex entries. I'm trying to use pybtex for parsing. First, I don't see that pybtex has a parse(input) option only a parse_file() option. This is what I'm doing currently:

def convert_to_modelform(bibtexb):
    parser = bibtex.Parser()
    f = open('/tmp/bibtex.bib', 'w')
    f.write(bibtexb)
    f.close
    bibdata = parser.parse_file('/tmp/bibtex.bib')
    print bibdata
    print len(bibdata.entries)
    return bibtexb

/tmp/bibtex.bib has the contents:

@article{article,
  author  = {Peter Adams},
  title   = {The title of the work},
  journal = {The name of the journal},
  year    = 1993,
  number  = 2,
  pages   = {201-213},
  month   = 7,
  note    = {An optional note},
  volume  = 4
}

print bibdata and print len(bibdata.entries) give me:

BibliographyData(entries=OrderedCaseInsensitiveDict({}), preamble=[])
0

What am I missing here?


回答1:


To parse from a string, use StringIO in combination with parse_stream:

import pybtex.database.input.bibtex
from StringIO import StringIO

def bibtex_string_to_data(s):
    parser = pybtex.database.input.bibtex.Parser()
    return parser.parse_stream(StringIO(s))

print bibtex_string_to_data("""
@article{article,
  author  = {Peter Adams},
  title   = {The title of the work},
  journal = {The name of the journal},
  year    = 1993,
  number  = 2,
  pages   = {201-213},
  month   = 7,
  note    = {An optional note},
  volume  = 4
}
""")

which gives (reformatted for readability):

BibliographyData(entries=OrderedCaseInsensitiveDict({
    'article': Entry(
        'article',
        fields={
            'volume': '4',
            'title': 'The title of the work',
            'journal': 'The name of the journal',
            'number': '2',
            'month': '7',
            'note': 'An optional note',
            'year': '1993',
            'pages': '201-213'},
        persons={
            'author': [Person(u'Adams, Peter')]})
    }), preamble=[])


来源:https://stackoverflow.com/questions/19306354/pybtex-does-not-recogonize-bibtex-entry

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