2013-10-10 46 views
2

我創建的出版物數據庫,允許用戶輸入bibtex條目,然後我解析和存儲在數據庫。現在,我無法解析bibtex條目。我正在嘗試使用pybtex進行解析。首先,我沒有看到pybtex只有parse_file()選項的解析(輸入)選項。這是我在做什麼目前:pybtex不recogonize bibtex條目

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 
} 

打印bibdata和打印LEN(bibdata.entries)給我:

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

我在這裏錯過了什麼?

回答

1

從字符串解析,結合parse_stream使用StringIO的:

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