Skip to content

Instantly share code, notes, and snippets.

@DancingQuanta
Forked from chaosct/cleanbib.py
Last active August 31, 2015 19:53
Show Gist options
  • Save DancingQuanta/39a2fc942c8438a20422 to your computer and use it in GitHub Desktop.
Save DancingQuanta/39a2fc942c8438a20422 to your computer and use it in GitHub Desktop.
Script to clean (and sort) the bibfile exported by Mendeley
# Script to clean (and sort) the bibfile exported by mendeley
# It is then apt for versioning
# Author: Carles F. Julià <carles.fernandez(AT)upf.edu>
# You will need bibtexparser package:
# $ pip install bibtexparser
# usage: cleanbib.py library.bib
# WARNING: it overwrites the original file
import bibtexparser
from bibtexparser.bparser import BibTexParser
if __name__ == '__main__':
import sys
bibfile = sys.argv[1]
with open(bibfile) as bibtex_file:
parser = BibTexParser()
bib_database = bibtexparser.load(bibtex_file, parser=parser)
for entry in bib_database.entries:
for k in ('file','annote','abstract'):
entry.pop(k,None)
bibtex_string = bibtexparser.dumps(bib_database)
with open(bibfile,'w') as bibtex_file:
bibtex_file.write(bibtex_string.encode('utf8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment