title: Importing/Exporting a nested dictionary from a CSV file
body: <p>So I have a CSV file with the data arranged like this:</p>
  
  <pre><code>X,a,1,b,2,c,3
  Y,a,1,b,2,c,3,d,4
  Z,l,2,m,3
  </code></pre>
  
  <p>I want to import the CSV to create a nested dictionary so that looks like this.</p>
  
  <pre><code>data = {'X' : {'a' : 1, 'b' : 2, 'c' : 3}, 
          'y' : {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4},
          'Z' : {'l' : 2, 'm' :3}}
  </code></pre>
  
  <p>After updating the dictionary in the program I wrote (I got that part figured out), I want to be able to export the dictionary onto the same CSV file, overwriting/updating it. However I want it to be in the same format as the previous CSV file so that I can import it again.</p>
  
  <p>I have been playing around with the import and have this so far</p>
  
  <pre><code>import csv
  data = {}
  with open('userdata.csv', 'r') as f:    
      reader = csv.reader(f)
      for row in reader:
         data[row[0]] = {row[i] for i in range(1, len(row))}
  </code></pre>
  
  <p>But this doesn't work as things are not arranged correctly. Some numbers are subkeys to other numbers, letters are out of place, etc. I haven't even gotten to the export part yet. Any ideas? </p>