Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trevismd/e97d85690ca82d65c41447c9f61c8137 to your computer and use it in GitHub Desktop.
Save trevismd/e97d85690ca82d65c41447c9f61c8137 to your computer and use it in GitHub Desktop.
Using the translation table from the Jabref program, finds and replaces all scientific journal names to their standardized abbreviated form. First argument is the file to be processed; outputs safely to 'abbreviated.bib'
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# Supporting Python 3 and JabRef csv file
import os
import re
import sys
try:
bibtexdb = open(sys.argv[1]).read()
except IndexError:
print("Error: specify the file to be processed!")
exit(1)
except FileNotFoundError:
print("Error: Provided filename could not be loaded!")
exit(1)
if not os.path.isfile('journalList.csv'):
import urllib.request
urllib.request.urlretrieve(
"https://raw.githubusercontent.com/JabRef/jabref/master/src/main/"
"resources/journals/journalList.csv",
filename="journalList.csv")
rulesfile = open('journalList.csv')
for rule in rulesfile.readlines():
patterns = rule.split(";")[:2]
pattern1, pattern2 = map(lambda pattern: "Journal = {%s}" % pattern,
patterns)
# avoid mere abbreviations
if pattern1 != pattern1.upper() and (' ' in pattern1):
repl = re.compile(re.escape(pattern1), re.IGNORECASE)
(bibtexdb, num_subs) = repl.subn(pattern2, bibtexdb)
if num_subs > 0:
print("Replaced (%ix) '%s' FOR '%s'" % (num_subs, *patterns))
with open('abbreviated.bib', 'w') as outfile:
outfile.write(bibtexdb)
print("Bibtex database with abbreviated files saved into 'abbreviated.bib'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment