{"id":5131,"library":"bibtexparser","title":"BibTeX Parser","description":"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.","status":"active","version":"1.4.4","language":"en","source_language":"en","source_url":"https://github.com/sciunto-org/python-bibtexparser","tags":["bibtex","parser","bibliography","academic","publication"],"install":[{"cmd":"pip install bibtexparser","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for parsing BibTeX files. Version 1.4.4 requires pyparsing>=3.0.0.","package":"pyparsing","optional":false}],"imports":[{"symbol":"read_file","correct":"import bibtexparser\n\nwith open('references.bib') as bibtex_file:\n    bib_database = bibtexparser.read_file(bibtex_file)"},{"symbol":"parse_string","correct":"import bibtexparser\n\nbib_string = \"@article{...}\"\nbib_database = bibtexparser.parse_string(bib_string)"},{"symbol":"dump","correct":"import bibtexparser\n\nwith open('output.bib', 'w') as bibtex_file:\n    bibtexparser.dump(bib_database, bibtex_file)"},{"note":"BibTexParser is located in the bparser submodule, not directly under bibtexparser.","wrong":"from bibtexparser import BibTexParser","symbol":"BibTexParser","correct":"from bibtexparser.bparser import BibTexParser"},{"note":"BibDatabase is located in the bibdatabase submodule, not directly under bibtexparser.","wrong":"from bibtexparser import BibDatabase","symbol":"BibDatabase","correct":"from bibtexparser.bibdatabase import BibDatabase"}],"quickstart":{"code":"import bibtexparser\nfrom bibtexparser.bibdatabase import BibDatabase\nfrom bibtexparser.bparser import BibTexParser\nimport os\n\n# Create a dummy .bib file for demonstration\ndummy_bib_content = \"\"\"\n@article{doe2023example,\n  title={An Example Article},\n  author={Doe, John and Smith, Jane},\n  journal={Journal of Examples},\n  year={2023},\n  volume={1},\n  number={1},\n  pages={1-10}\n}\n@book{smith2022guide,\n  title={A Guide to Everything},\n  author={Smith, Jane},\n  publisher={Example Press},\n  year={2022}\n}\n\"\"\"\n\n# Ensure the file exists for the example\nwith open('temp_references.bib', 'w') as f:\n    f.write(dummy_bib_content)\n\n# 1. Read a BibTeX file\nwith open('temp_references.bib') as bibtex_file:\n    # Using the default parser\n    bib_database = bibtexparser.read_file(bibtex_file)\n\nprint(\"--- Parsed Database ---\")\nfor entry in bib_database.entries:\n    print(f\"ID: {entry['ID']}, Title: {entry.get('title')}\")\n\n# 2. Access and modify entries\nif bib_database.entries:\n    first_entry = bib_database.entries[0]\n    print(f\"\\nFirst entry authors: {first_entry.get('author')}\")\n    first_entry['note'] = 'Added by example'\n    print(f\"First entry new note: {first_entry.get('note')}\")\n\n# 3. Create a new BibDatabase and add an entry\nnew_bib = BibDatabase()\nnew_bib.entries.append({\n    'ID': 'example2024new',\n    'ENTRYTYPE': 'misc',\n    'title': 'New Entry Added',\n    'year': '2024'\n})\n\n# 4. Dump (write) to a new BibTeX file\nwith open('output.bib', 'w') as output_file:\n    bibtexparser.dump(new_bib, output_file)\n\nprint(\"\\nNew entry saved to output.bib\")\n\n# Clean up dummy files\nos.remove('temp_references.bib')\nos.remove('output.bib')\n","lang":"python","description":"This quickstart demonstrates how to read a BibTeX file, access and modify entries within the `BibDatabase` object, and then write the data back to a new file. It covers both reading with `bibtexparser.read_file` and writing with `bibtexparser.dump`, as well as direct manipulation of `BibDatabase` objects."},"warnings":[{"fix":"Consult the 2.x migration guide upon its release. For now, stick to 1.x.x for production code or thoroughly test beta versions.","message":"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.","severity":"breaking","affected_versions":"2.0.0bX and later (upcoming 2.0.0 stable)"},{"fix":"Use explicit submodule imports: `from bibtexparser.bparser import BibTexParser` and `from bibtexparser.bibdatabase import BibDatabase`.","message":"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.","severity":"gotcha","affected_versions":"1.x.x"},{"fix":"Ensure `pyparsing` is updated to version 3.0.0 or higher (`pip install --upgrade pyparsing`).","message":"Version 1.4.4 explicitly requires `pyparsing>=3.0.0`. Older versions of `pyparsing` might lead to import errors or parsing issues.","severity":"gotcha","affected_versions":"1.4.4"},{"fix":"Use `entry.get('field_name', default_value)` for safe access to fields that might be optional or missing from some entries.","message":"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.","severity":"gotcha","affected_versions":"1.x.x"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}