Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2014 18:25
Show Gist options
  • Save anonymous/10299866 to your computer and use it in GitHub Desktop.
Save anonymous/10299866 to your computer and use it in GitHub Desktop.
Format a CSV in a somewhat more readable way.
import pandas as pd
import sys
filename, outfile = sys.argv[1:3]
responses = pd.read_csv(filename)
def format_row(row):
return '\n'.join(["<p><b>{}</b><br>{}</p>".format(k, v.replace('\n', '<br>')) for k, v in row.iteritems()])
def format_all(responses):
return '\n'.join('<article style="columns: 2; -moz-columns: 2; line-height: 1.5; padding: 1em 0; border-bottom: 1px dashed black">{}</article>'.format(format_row(row)) for idx, row in responses.iterrows())
page = '<meta charset="utf8">' + format_all(responses)
with open(outfile, 'wb') as f:
f.write(page.encode('utf8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment