问题
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