Convert BibTex file to database entries using Python

后端 未结 5 1582
星月不相逢
星月不相逢 2021-02-02 15:41

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).

After doing some initial re

5条回答
  •  醉话见心
    2021-02-02 15:50

    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
    

提交回复
热议问题