Bibtex不承认bibtex进入

时间:2013-10-10 21:15:46

标签: python-2.7 bibtex

我正在创建一个发布数据库,允许用户输入bibtex条目,然后我将其解析并存储在数据库中。现在,我在解析bibtex条目时遇到了麻烦。我正在尝试使用pybtex进行解析。首先,我没有看到pybtex只有parse_file()选项的解析(输入)选项。这就是我目前正在做的事情:

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的内容为:

@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和print len(bibdata.entries)给我:

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

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

要从字符串解析,请将StringIO与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
}
""")

给出(重新格式化以便于阅读):

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=[])