BibTeX Parser
bibtexparser is a Python 3 library for parsing and writing BibTeX files. It provides a simple API to read, modify, and dump BibTeX data. The current stable version is 1.4.4, with active development on a 2.0.0 beta series introducing significant changes. Releases are somewhat irregular, with bugfix and feature releases for the 1.x branch and active development on 2.x.
Warnings
- breaking Version 2.0.0 (currently in beta) introduces significant breaking changes. Key changes include the renaming of `entry.pop_field` to `entry.pop`, changes in default arguments for middleware, and potential renames for `write_file` parameters. Code written for 1.x.x will likely not work with 2.x.x without modifications.
- gotcha The `BibTexParser` class and `BibDatabase` class are located in submodules (`bibtexparser.bparser` and `bibtexparser.bibdatabase` respectively) and cannot be directly imported from the top-level `bibtexparser` module.
- gotcha Version 1.4.4 explicitly requires `pyparsing>=3.0.0`. Older versions of `pyparsing` might lead to import errors or parsing issues.
- gotcha The BibTeX entries are stored as dictionaries within the `bib_database.entries` list. Accessing fields like 'title' or 'author' should be done using dictionary syntax (`entry['title']` or `entry.get('title')`). Missing keys will raise a `KeyError` if accessed directly, so `get()` is often safer.
Install
-
pip install bibtexparser
Imports
- read_file
import bibtexparser with open('references.bib') as bibtex_file: bib_database = bibtexparser.read_file(bibtex_file) - parse_string
import bibtexparser bib_string = "@article{...}" bib_database = bibtexparser.parse_string(bib_string) - dump
import bibtexparser with open('output.bib', 'w') as bibtex_file: bibtexparser.dump(bib_database, bibtex_file) - BibTexParser
from bibtexparser.bparser import BibTexParser
- BibDatabase
from bibtexparser.bibdatabase import BibDatabase
Quickstart
import bibtexparser
from bibtexparser.bibdatabase import BibDatabase
from bibtexparser.bparser import BibTexParser
import os
# Create a dummy .bib file for demonstration
dummy_bib_content = """
@article{doe2023example,
title={An Example Article},
author={Doe, John and Smith, Jane},
journal={Journal of Examples},
year={2023},
volume={1},
number={1},
pages={1-10}
}
@book{smith2022guide,
title={A Guide to Everything},
author={Smith, Jane},
publisher={Example Press},
year={2022}
}
"""
# Ensure the file exists for the example
with open('temp_references.bib', 'w') as f:
f.write(dummy_bib_content)
# 1. Read a BibTeX file
with open('temp_references.bib') as bibtex_file:
# Using the default parser
bib_database = bibtexparser.read_file(bibtex_file)
print("--- Parsed Database ---")
for entry in bib_database.entries:
print(f"ID: {entry['ID']}, Title: {entry.get('title')}")
# 2. Access and modify entries
if bib_database.entries:
first_entry = bib_database.entries[0]
print(f"\nFirst entry authors: {first_entry.get('author')}")
first_entry['note'] = 'Added by example'
print(f"First entry new note: {first_entry.get('note')}")
# 3. Create a new BibDatabase and add an entry
new_bib = BibDatabase()
new_bib.entries.append({
'ID': 'example2024new',
'ENTRYTYPE': 'misc',
'title': 'New Entry Added',
'year': '2024'
})
# 4. Dump (write) to a new BibTeX file
with open('output.bib', 'w') as output_file:
bibtexparser.dump(new_bib, output_file)
print("\nNew entry saved to output.bib")
# Clean up dummy files
os.remove('temp_references.bib')
os.remove('output.bib')