A comprehensive BibTeX parser library for Python 3 that enables parsing and writing of bibliographic data files
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Simple interface for parsing BibTeX strings and files into BibDatabase objects, and writing them back to BibTeX format. These functions provide the most straightforward way to work with BibTeX data and handle the majority of common use cases.
Parse BibTeX data from a string into a structured BibDatabase object. This is useful when you have BibTeX data in memory or received from an API.
def loads(bibtex_str: str, parser=None) -> BibDatabase:
"""
Load BibDatabase object from a BibTeX string.
Parameters:
- bibtex_str (str): Input BibTeX string to be parsed
- parser (BibTexParser, optional): Custom parser to use instead of default
Returns:
BibDatabase: Parsed bibliographic database object
Example:
>>> import bibtexparser
>>> bibtex_str = '@article{key, title={Example}}'
>>> db = bibtexparser.loads(bibtex_str)
>>> len(db.entries)
1
"""Parse BibTeX data directly from a file object. This is the most common way to load existing BibTeX files.
def load(bibtex_file, parser=None) -> BibDatabase:
"""
Load BibDatabase object from a BibTeX file.
Parameters:
- bibtex_file (file): Input file object to be parsed
- parser (BibTexParser, optional): Custom parser to use instead of default
Returns:
BibDatabase: Parsed bibliographic database object
Example:
>>> import bibtexparser
>>> with open('references.bib') as f:
... db = bibtexparser.load(f)
>>> print(f"Loaded {len(db.entries)} entries")
"""Convert a BibDatabase object back to a BibTeX-formatted string. Useful for generating BibTeX output in memory.
def dumps(bib_database: BibDatabase, writer=None) -> str:
"""
Convert BibDatabase object to a BibTeX-formatted string.
Parameters:
- bib_database (BibDatabase): Bibliographic database object to convert
- writer (BibTexWriter, optional): Custom writer to use instead of default
Returns:
str: BibTeX-formatted string
Example:
>>> import bibtexparser
>>> db = bibtexparser.BibDatabase()
>>> db.entries = [{'ENTRYTYPE': 'article', 'ID': 'key', 'title': 'Example'}]
>>> bibtex_str = bibtexparser.dumps(db)
>>> print(bibtex_str)
"""Write a BibDatabase object directly to a file. This is the standard way to save bibliographic data.
def dump(bib_database: BibDatabase, bibtex_file, writer=None) -> None:
"""
Write BibDatabase object to a BibTeX file.
Parameters:
- bib_database (BibDatabase): Bibliographic database object to write
- bibtex_file (file): Output file object to write to
- writer (BibTexWriter, optional): Custom writer to use instead of default
Returns:
None
Example:
>>> import bibtexparser
>>> db = bibtexparser.BibDatabase()
>>> db.entries = [{'ENTRYTYPE': 'article', 'ID': 'key', 'title': 'Example'}]
>>> with open('output.bib', 'w') as f:
... bibtexparser.dump(db, f)
"""import bibtexparser
# Read BibTeX from file
with open('input.bib') as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
# Process entries (example: add a note to each entry)
for entry in bib_database.entries:
entry['note'] = 'Processed by bibtexparser'
# Write back to new file
with open('output.bib', 'w') as bibtex_file:
bibtexparser.dump(bib_database, bibtex_file)import bibtexparser
bibtex_str = """
@article{Einstein1905,
title={On the electrodynamics of moving bodies},
author={Einstein, Albert},
journal={Annalen der Physik},
year={1905}
}
"""
# Parse from string
bib_database = bibtexparser.loads(bibtex_str)
# Access data
entry = bib_database.entries[0]
print(f"Title: {entry['title']}")
print(f"Author: {entry['author']}")
# Convert back to string
output_str = bibtexparser.dumps(bib_database)
print(output_str)import bibtexparser
try:
with open('references.bib') as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
print(f"Successfully loaded {len(bib_database.entries)} entries")
except FileNotFoundError:
print("BibTeX file not found")
except Exception as e:
print(f"Error parsing BibTeX: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-bibtexparser