title: Programmatically convert pandas dataframe to markdown table
body: <p>I have a Pandas Dataframe generated from a database, which has data with mixed encodings. For example:</p>
  
  <pre><code>+----+-------------------------+----------+------------+------------------------------------------------+--------------------------------------------------------+--------------+-----------------------+
  | ID | path                    | language | date       | longest_sentence                               | shortest_sentence                                      | number_words | readability_consensus |
  +----+-------------------------+----------+------------+------------------------------------------------+--------------------------------------------------------+--------------+-----------------------+
  | 0  | data/Eng/Sagitarius.txt | Eng      | 2015-09-17 | With administrative experience in the prepa... | I am able to relocate internationally on short not...  | 306          | 11th and 12th grade   |
  +----+-------------------------+----------+------------+------------------------------------------------+--------------------------------------------------------+--------------+-----------------------+
  | 31 | data/Nor/Høylandet.txt  | Nor      | 2015-07-22 | Høgskolen i Østfold er et eksempel...          | Som skuespiller har jeg både...                        | 253          | 15th and 16th grade   |
  +----+-------------------------+----------+------------+------------------------------------------------+--------------------------------------------------------+--------------+-----------------------+
  </code></pre>
  
  <p>As seen there is a mix of English and Norwegian (encoded as ISO-8859-1 in the database I think). I need to get the contents of this Dataframe output as a Markdown table, but without getting problems with encoding. I followed <a href="https://stackoverflow.com/a/15445930/603387">this answer</a> (from the question <a href="https://stackoverflow.com/questions/13394140/generate-markdown-tables">Generate Markdown tables?</a>) and got the following:</p>
  
  <pre><code>import sys, sqlite3
  
  db = sqlite3.connect("Applications.db")
  df = pd.read_sql_query("SELECT path, language, date, longest_sentence, shortest_sentence, number_words, readability_consensus FROM applications ORDER BY date(date) DESC", db)
  db.close()
  
  rows = []
  for index, row in df.iterrows():
      items = (row['date'], 
               row['path'], 
               row['language'], 
               row['shortest_sentence'],
               row['longest_sentence'], 
               row['number_words'], 
               row['readability_consensus'])
      rows.append(items)
  
  headings = ['Date', 
              'Path', 
              'Language',
              'Shortest Sentence', 
              'Longest Sentence since', 
              'Words',
              'Grade level']
  
  fields = [0, 1, 2, 3, 4, 5, 6]
  align = [('^', '&lt;'), ('^', '^'), ('^', '&lt;'), ('^', '^'), ('^', '&gt;'),
           ('^','^'), ('^','^')]
  
  table(sys.stdout, rows, fields, headings, align)
  </code></pre>
  
  <p>However, this yields an <code>UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 72: ordinal not in range(128)</code> error. How can I output the Dataframe as a Markdown table? That is, for the purpose of storing this code in a file for use in writing a Markdown document. I need the output to look like this:</p>
  
  <pre><code>| ID | path                    | language | date       | longest_sentence                               | shortest_sentence                                      | number_words | readability_consensus |
  |----|-------------------------|----------|------------|------------------------------------------------|--------------------------------------------------------|--------------|-----------------------|
  | 0  | data/Eng/Sagitarius.txt | Eng      | 2015-09-17 | With administrative experience in the prepa... | I am able to relocate internationally on short not...  | 306          | 11th and 12th grade   |
  | 31 | data/Nor/Høylandet.txt  | Nor      | 2015-07-22 | Høgskolen i Østfold er et eksempel...          | Som skuespiller har jeg både...                        | 253          | 15th and 16th grade   |
  </code></pre>