title: Converting CSV to Json in Python
body: <p>I have an excel spreadsheet(saved as a csv file) of four columns. The first and third columns contain words, the second and fourth columns contain frequency. So it looks something like this:</p>
  
  <pre><code>word1, freq1, word2, freq2
  word3, freq3, word4, freq4
  </code></pre>
  
  <p>...and so on</p>
  
  <p>I have the following code that converts the csv to the json file.</p>
  
  <pre><code>import csv
  import json
  
  csvfile = open('sample.csv', 'r')
  jsonfile = open('sample.json', 'w')
  
  
  fieldnames = ("feature","r", "feature","r")
  reader = csv.DictReader(csvfile, fieldnames)
  out = json.dumps( [ row for row in reader ] )
  jsonfile.write(out)
  </code></pre>
  
  <p>Really simple         stuff. 
  This, however, returns a json file that looks like this </p>
  
  <pre><code>[{"r" : freq2 "feature" : "word2"} {"r" : freq1 "feature" : "word1"}{"r" : freq4 "feature" : "word4"}{"r" : freq3 "feature" : "word3"}]
  </code></pre>
  
  <p>I'm trying to figure out a way so that the returning json file will look like this</p>
  
  <pre><code>[{"word1" : freq1}{"word2" :freq2}{"word3" :freq3}{"word4" :freq4}]
  </code></pre>
  
  <p>In another words, I want to make column one of my spread sheet the key of column 2, and column 3 the key for column 4.</p>