Skip to content

Instantly share code, notes, and snippets.

@ngoffee
Created January 25, 2013 01:13
Show Gist options
  • Save ngoffee/4630655 to your computer and use it in GitHub Desktop.
Save ngoffee/4630655 to your computer and use it in GitHub Desktop.
Convert a Goodreads csv export to headings and itemized lists in Markdown
#!/usr/bin/env python
import csv
import re
import sys
from collections import namedtuple
def oxford_join(seq):
if len(seq) == 1:
return seq
elif len(seq) == 2:
return '%s and %s' % (seq[0], seq[1])
else:
return '%s, and %s' % (', '.join(seq[:-1]), seq[-1])
def main(path):
booklist = {}
with open(path) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
author = row['Author l-f']
add_authors = row['Additional Authors']
if add_authors:
add_authors = re.split('r,\s*', add_authors)
#author = ', '.join([author] + add_authors)
author = oxford_join([author] + add_authors)
title = row['Title']
shelves = set(re.split(r',\s*', row['Bookshelves']))
excshelf = row['Exclusive Shelf']
if excshelf == 'read':
shelves = set(['~FINISHED'])
else:
shelves.remove(excshelf)
recommended = row['Recommended By']
book = author, title, recommended
if not shelves:
shelves = set(['uncategorized'])
for shelf in shelves:
if shelf not in booklist:
booklist[shelf] = []
booklist[shelf].append(book)
for shelf in sorted(booklist.keys()):
print
if isinstance(shelf, basestring):
cap_shelf = "%s%s" % (shelf[0].upper(), shelf[1:])
else:
cap_shelf = shelf
print "# %s" % cap_shelf
for author, title, recommended in sorted(booklist[shelf]):
author = author.rstrip('.')
if title[-1] not in ('.', '?', '!'):
title = '%s.' % title
if recommended:
recommended = ' (Recommended by %s.)' % recommended
print "- %s. %s%s" % (author, title, recommended)
if __name__ == '__main__':
main(sys.argv[1])
@gwern
Copy link

gwern commented Sep 11, 2013

Incidentally, I've written a GoodReads CSV-Markdown compiler using Pandoc; you can see the results here.

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