syrte
12/31/2016 - 1:39 PM

Fix the improper year in bibtex exported from zotero which is reported [here]( https://github.com/retorquere/zotero-better-bibtex/issues/621

Fix the improper year in bibtex exported from zotero which is reported here.

#!/usr/bin/env python
import sys
import bibtexparser

if len(sys.argv) == 2:
    fn = sys.argv[-1]
    fout = None
elif len(sys.argv) == 3 and sys.argv[1] == "-i":
    fn = sys.argv[-1]
    fout = fn
else:
    print("""usage:
    clean_bibtex input.bib >output.bib
    clean_bibtex -i input.bib""")
    sys.exit()

bib = bibtexparser.loads(open(fn).read())
for key, entry in bib.entries_dict.items():
    if 'year' in entry:
        entry['year'] = entry['year'].strip().split()[-1]
    else:
        print("warning: no year in '%s'\n%s\n" % (key, entry))
        #del bib.entries_dict[key]

output = bibtexparser.dumps(bib)
if fout is None:
    print(output)
else:
    open(fout, 'w').write(output.encode('utf8'))