使用 Python 将 BibTex 文件转换为数据库条目 [英] Convert BibTex file to database entries using Python

查看:64
本文介绍了使用 Python 将 BibTex 文件转换为数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 bibTex 文件,我需要将相应的字段(作者、标题、期刊等)添加到 MySQL 数据库(具有自定义架构)中的表中.

Given a bibTex file, I need to add the respective fields(author, title, journal etc.) to a table in a MySQL database (with a custom schema).

在做了一些初步研究后,我发现存在 Bibutils 我可以用于将 bib 文件转换为 xml.我最初的想法是将其转换为 XML,然后在 python 中解析 XML 以填充字典.

After doing some initial research, I found that there exists Bibutils which I could use to convert a bib file to xml. My initial idea was to convert it to XML and then parse the XML in python to populate a dictionary.

我的主要问题是:

  1. 有没有更好的方法可以进行这种转换?
  2. 是否有直接解析 bibTex 并在 python 中给我字段的库?

(我确实找到了 bibliography.parsing,它在内部使用了 bibutils但是关于它的文档并不多,而且我发现很难让它工作).

(I did find bibliography.parsing, which uses bibutils internally but there is not much documentation on it and am finding it tough to get it to work).

推荐答案

老问题,但我目前正在使用 Pybtex 库,它有一个内置的解析器:

Old question, but I am doing the same thing at the moment using the Pybtex library, which has an inbuilt parser:

from pybtex.database.input import bibtex

#open a bibtex file
parser = bibtex.Parser()
bibdata = parser.parse_file("myrefs.bib")

#loop through the individual references
for bib_id in bibdata.entries:
    b = bibdata.entries[bib_id].fields
    try:
        # change these lines to create a SQL insert
        print b["title"]
        print b["journal"]
        print b["year"]
        #deal with multiple authors
        for author in bibdata.entries[bib_id].persons["author"]:
            print author.first(), author.last()
    # field may not exist for a reference
    except(KeyError):
        continue

这篇关于使用 Python 将 BibTex 文件转换为数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆