Skip to content

Instantly share code, notes, and snippets.

@jwise77
Created May 21, 2021 18:33
Show Gist options
  • Save jwise77/dd97d7d84e633bb50b518331bbbe797d to your computer and use it in GitHub Desktop.
Save jwise77/dd97d7d84e633bb50b518331bbbe797d to your computer and use it in GitHub Desktop.
def uniq(seq):
set = {}
map(set.__setitem__, seq, [])
return set.keys()
#------------------------------------------------------------------------
from string import join
datfile = 'rawbib.dat'
output = 'bibliography.tex'
lines = open(datfile).readlines()
nocomments = []
for line in lines:
if line.find('%') == -1:
nocomments.append(line)
lines = nocomments
entry_lines = []
count = 0
for line in lines:
if line.startswith('\\bibitem'):
entry_lines.append(count)
count += 1
nentry = len(entry_lines)
print "Read %d entries in %s." % (nentry, datfile)
entries = {}
for i in range(nentry):
i0 = entry_lines[i]
if i < nentry-1:
i1 = entry_lines[i+1]-1
else:
i1 = len(lines)
# print "%d/%d, %d/%d\n" % (i, nentry, i1, len(lines))
thisEntry = join(lines[i0:i1])
# Find the identifier
tempStr = thisEntry.split(']')[1]
tempStr = tempStr.split('{')[1]
ident = tempStr.split('}')[0]
# If not in database, store it.
if not entries.has_key(ident):
entries[ident] = thisEntry
sorted_ids = sorted(entries, key=str.lower)
fptr = open(output, 'w')
fptr.write('\\bibliography{suthesis}\n')
fptr.write('\\addcontentsline{toc}{chapter}{Bibliography}\n')
fptr.write('\n')
fptr.write('\\begin{thebibliography}{}\n')
fptr.write('\n')
for ident in sorted_ids:
fptr.write(entries[ident])
fptr.write('\n')
fptr.write('\n')
fptr.write('\\end{thebibliography}{}\n')
fptr.close()
print "Wrote %d entries with unique identifiers in %s" % \
(len(sorted_ids), output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment