Skip to content

Instantly share code, notes, and snippets.

@araastat
Created December 6, 2013 07:37
Show Gist options
  • Save araastat/7819975 to your computer and use it in GitHub Desktop.
Save araastat/7819975 to your computer and use it in GitHub Desktop.
#!/home/abhijit/anaconda/bin/python
import re
def getauthor(x):
authlist = x.split('.')[0]
bl = authlist.split(',')
bl = [u.lstrip().replace(' ',', ') for u in bl]
bl = [u.replace('et al','"et al"') for u in bl]
out = 'author = {'+' and '.join(bl)+'}'
return out
def getvol(x,pos=2):
a = x.split('.')[pos]
out = a[a.find(';'):a.find(':')].lstrip(';')
return out
f = open('Bibliography_ToddArnedtGrant.txt','r')
x = f.readlines()
f.close()
x = [u.rstrip('\r\n') for u in x]
x = [u for u in x if re.search('^[0-9]',u) is not None]
x = [u.split('\t')[1] for u in x]
y = [u.split('.')[:-1] for u in x]
outliers = [x[i] for i in range(len(x)) if len(y[i]) != 3]
goodx = list(set(x).difference(set(outliers)))
books = [u for u in goodx if re.search('\d{4}\.$', u) is not None]
articles = list(set(goodx).difference(set(books)))
g = file('Todd.bib','w')
# Articles first
authors = [getauthor(u) for u in articles]
title = ['title = {'+u.split('.')[1].lstrip()+'}' for u in articles]
journal = ['journal = {' + re.split('\d{4}',u.split('.')[2])[0].strip()+'}' for u in articles]
pages = ['pages = {'+u.split('.')[2].split(':')[-1].rstrip('.')+'}' for u in articles]
vols = ['volume = {'+getvol(u)+'}' for u in articles]
year = ['year = {'+ re.findall('\d{4}',u.split('.')[2])[0]+'}' for u in articles]
kw = [u[0][u[0].find('{'):u[0].find(',')].lstrip('{')+re.findall('\d{4}',u[1])[0] for u in zip(authors, year)]
bibs = [',\n\t'.join(u) for u in zip(kw, authors, title, journal, pages, vols,year)]
bibs = ['@article{'+u+'}\n\n' for u in bibs]
g.writelines(bibs)
#Books next
authors = [getauthor(u) for u in books]
title = ['title = {'+u.split('.')[1].lstrip()+'}' for u in books]
year = ['year = {'+ re.findall('\d{4}',u.split('.')[2])[0]+'}' for u in books]
publisher = [re.split(' \d{4}',u.split('.')[2])[0] for u in books]
publisher = ['publisher = {'+ u.lstrip().rstrip(',')+'}' for u in publisher]
kw = [u[0][u[0].find('{'):u[0].find(',')].lstrip('{')+re.findall('\d{4}',u[1])[0] for u in zip(authors, year)]
bibs = [',\n\t'.join(u) for u in zip(kw, authors, title, publisher, year)]
bibs = ['@book{'+u+'}\n\n' for u in bibs]
g.writelines(bibs)
g.close()
g = open('ToddOutliers.txt','w')
g.writelines([u+'\n\n' for u in outliers])
g.close()
#---------------------
f = open('Bibliography_SoniaDuffyGrant.txt','r')
x = f.readlines()
f.close()
x = [u.rstrip('\r\n') for u in x]
x = [u for u in x if re.search('^\t[0-9]',u) is not None]
x = [u.split('\t')[2] for u in x]
y = [u.split('.')[:-1] for u in x]
outliers = [x[i] for i in range(len(x)) if len(y[i]) != 4]
goodx = list(set(x).difference(set(outliers)))
books = [u for u in goodx if re.search('\d{4}\.$', u) is not None and re.search('-',u.split('.')[-2]) is None]
articles = list(set(goodx).difference(set(books)))
outliers.append(articles[59])
articles.remove(articles[59])
outliers.append(books[1])
books.remove(books[1])
g = file('Sonia.bib','w')
# Articles first
authors = [getauthor(u) for u in articles]
title = ['title = {'+u.split('.')[1].lstrip()+'}' for u in articles]
journal = ['journal = {' + u.split('.')[2].strip()+'}' for u in articles]
pages = ['pages = {'+u.split('.')[3].split(':')[-1].rstrip('.')+'}' for u in articles]
vols = ['volume = {'+getvol(u,3)+'}' for u in articles]
yr = [re.findall('\d{4}', u.split('.')[3]) for u in articles]
yr[[len(u) for u in yr].index(0)] = ['']
year = ['year = {'+ u[0]+'}' for u in yr]
kw = [u[0][u[0].find('{'):u[0].find(',')].lstrip('{')+u[1][0] for u in zip(authors, yr)]
bibs = [',\n\t'.join(u) for u in zip(kw, authors, title, journal, pages, vols,year)]
bibs = ['@article{'+u+'}\n\n' for u in bibs]
g.writelines(bibs)
#Books next
authors = [getauthor(u) for u in books]
title = ['title = {'+u.split('.')[1].lstrip()+'}' for u in books]
year = ['year = {'+ re.findall('\d{4}',u.split('.')[-2])[0]+'}' for u in books]
publisher = [re.split(' \d{4}',u.split('.')[2])[0] for u in books]
publisher = ['publisher = {'+ u.lstrip().rstrip(',')+'}' for u in publisher]
kw = [u[0][u[0].find('{'):u[0].find(',')].lstrip('{')+re.findall('\d{4}',u[1])[0] for u in zip(authors, year)]
bibs = [',\n\t'.join(u) for u in zip(kw, authors, title, publisher, year)]
bibs = ['@book{'+u+'}\n\n' for u in bibs]
g.writelines(bibs)
g.close()
g = open('SoniaOutliers.txt','w')
g.writelines([u+'\n\n' for u in outliers])
g.close()
@araastat
Copy link
Author

araastat commented Dec 6, 2013

This is a Python script to convert Word-based bibliographies to bibtex, for loading into Papers. This script is very rudimentary.

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