Pybtex不会重新组合bibtex条目 [英] Pybtex does not recogonize bibtex entry
问题描述
我正在创建一个出版物数据库,允许用户输入bibtex条目,然后将其解析并存储在数据库中。目前,我在解析bibtex条目时遇到了麻烦。我正在尝试使用pybtex进行解析。首先,我看不到pybtex只有parse_file()选项具有parse(input)选项。这是我目前正在做的事情:
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包含以下内容:
/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
}
打印bibdata和打印len(bibdata.entries)给我:
print bibdata and print len(bibdata.entries) give me:
BibliographyData(entries=OrderedCaseInsensitiveDict({}), preamble=[])
0
我在这里想念什么?
推荐答案
要从字符串中进行解析,请结合使用StringIO和parse_stream:
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=[])
这篇关于Pybtex不会重新组合bibtex条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!