Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
import sys, os, re
try: bibtexdb = open(sys.argv[1]).read()
except: print("Error: specify the file to be processed!")
if not os.path.isfile('journalList.txt'):
import urllib
urllib.urlretrieve("https://raw.githubusercontent.com/JabRef/jabref/master/src/main/resources/journals/journalList.txt",
filename="journalList.txt")
rulesfile = open('journalList.txt')
for rule in rulesfile.readlines()[::-1]: ## reversed alphabetical order matches extended journal names first
pattern1, pattern2 = rule.strip().split(" = ")
if pattern1 != pattern1.upper() and (' ' in pattern1): ## avoid mere abbreviations
#bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip()) ## problem - this is case sensitive
repl = re.compile(re.escape(pattern1), re.IGNORECASE) ## this is more robust, although ca. 10x slower
(bibtexdb, num_subs) = repl.subn(pattern2, bibtexdb)
if num_subs > 0:
print "Replacing '%s' FOR '%s'" % (pattern1, pattern2)
with open('abbreviated.bib', 'w') as outfile:
outfile.write(bibtexdb)
print "Bibtex database with abbreviated files saved into 'abbreviated.bib'"
@peci1

This comment has been minimized.

Copy link

@peci1 peci1 commented May 10, 2016

Check out my latest revision: https://gist.github.com/peci1/4e67f3d0521ce014fc952bcca664b37d/revisions

The text "Replacing..." is only printed when there is at least one successful match for the pattern.

@jskDr

This comment has been minimized.

Copy link

@jskDr jskDr commented Oct 17, 2016

You need to add: import re
Also, to be used in Python 3.0. your print statement should have ().

@FilipDominec

This comment has been minimized.

Copy link
Owner Author

@FilipDominec FilipDominec commented Nov 2, 2016

Thank you all. 👍

@thorade

This comment has been minimized.

Copy link

@thorade thorade commented Feb 16, 2017

Lines 23 and 27 still use print as a statement, but should use it like a function with parantheses.
Also have a look at this awesome resource:
https://pyformat.info/
I highliy recommend the "new" format syntax.

@kuangyia

This comment has been minimized.

Copy link

@kuangyia kuangyia commented Dec 26, 2017

When I using this script in my windows laptop. It gives an error like this:
for rule in rulesfile.readlines()[::-1]: ## reversed alphabetical order matches extended journal names first
UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 7474: illegal multibyte sequence

So what happens ? Thanks.

@bergerrjf

This comment has been minimized.

Copy link

@bergerrjf bergerrjf commented Jan 19, 2020

It seems there is no https://raw.githubusercontent.com/JabRef/jabref/master/src/main/resources/journals/journalList.txt any more. I obtain

6b6b>./Abbreviate\ Journal\ Names\ in\ Bibtex\ Database.py bib.bib
Traceback (most recent call last):
File "./Abbreviate Journal Names in Bibtex Database.py", line 17, in
pattern1, pattern2 = rule.strip().split(" = ")
ValueError: need more than 1 value to unpack

@FilipDominec

This comment has been minimized.

Copy link
Owner Author

@FilipDominec FilipDominec commented Jan 19, 2020

@bergerrjf, you are right, the file has disappeared. I can look for a cached version in my computer, though.

@bergerrjf

This comment has been minimized.

Copy link

@bergerrjf bergerrjf commented Jan 19, 2020

That would be cool!

@glucksfall

This comment has been minimized.

@bergerrjf

This comment has been minimized.

Copy link

@bergerrjf bergerrjf commented Jan 21, 2020

Yes it seems so! Though its in csv and not txt.

@glucksfall

This comment has been minimized.

Copy link

@glucksfall glucksfall commented Jan 21, 2020

csv is a txt file. Nothing to worry about.

However, there are changes that make the script useless. I had to change ";" to " = ", remove the ";;" at the end of each line, and remove some odd lines with more than one "=" symbol. Besides that, the script works fine just for one thing: If the journal is also the title of a paper (What is systems biology? -> What is Syst. Biol.?)

I thank you, @FilipDominec for your wonderful work.

@FilipDominec

This comment has been minimized.

Copy link
Owner Author

@FilipDominec FilipDominec commented Jan 24, 2020

For anybody having trouble with the new script referenced by @glucksfall, I attach the good old journalList.txt: https://gist.github.com/FilipDominec/6df14b3424e335c4a47a96640f7f0df9

@trevismd

This comment has been minimized.

Copy link

@trevismd trevismd commented Jun 4, 2020

This was of great help, thanks @FilipDominec !
I updated it to directly process the JabRef CSV and solve the issue mentioned by @glucksfall as well as another one I had, where a subset of the journal name was matched to another journal and then only part of it was abbreviated ("Journal of Medicine" being alphabetically before "New England Journal of Medicine").
I basically updated the regex to find and replace Journal = {pattern}.

@FilipDominec

This comment has been minimized.

Copy link
Owner Author

@FilipDominec FilipDominec commented Jan 28, 2021

I am happy that you find this little script useful. Perhaps should I somehow sync all the improvements in at least 4 modified forks?

@FilipDominec

This comment has been minimized.

Copy link
Owner Author

@FilipDominec FilipDominec commented Jan 28, 2021

@arahatashun

This comment has been minimized.

Copy link

@arahatashun arahatashun commented Jan 28, 2021

I found @trevismd's python3 version of code after I posted my code so I deleted mine.

Anyway, providing python3 code explicitly will be helpful for people who come up with this gist.

By the way, what is journalList.txt based on? A journal I try to submit to requires to follow ISSN List of Title Word Abbreviations so I want to know that.
It seems the list is based on JabRef.

@trevismd

This comment has been minimized.

Copy link

@trevismd trevismd commented Jan 30, 2021

@FilipDominec, feel free to reuse any part of my contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment