Skip to content

Instantly share code, notes, and snippets.

@mmakowski
Created August 29, 2019 08:03
Show Gist options
  • Save mmakowski/8d5e1a05e4ba1115351288e346f4c0dc to your computer and use it in GitHub Desktop.
Save mmakowski/8d5e1a05e4ba1115351288e346f4c0dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import bibtexparser
from bibtexparser.bparser import BibTexParser
FIELDS_TO_KEEP = {
'*': ['title', 'author', 'year', 'pages', 'ENTRYTYPE', 'ID'],
'article': ['volume', 'number', 'journaltitle', 'shortjournal']
}
def main(bibtex_file):
with open(bibtex_file) as f:
bib = bibtexparser.load(f, BibTexParser(ignore_nonstandard_types=False))
for entry in bib.entries:
_add_year(entry)
_filter_fields(entry)
print(bibtexparser.dumps(bib))
def _add_year(entry):
if 'year' not in entry and 'date' in entry:
entry['year'] = entry['date'][:4]
def _filter_fields(entry):
fields_to_keep = FIELDS_TO_KEEP['*'] + FIELDS_TO_KEEP.get(entry['ENTRYTYPE'], [])
for field in list(entry.keys()):
if field not in fields_to_keep:
del entry[field]
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment