title: Python CSV: write rows according to dict mapping
body: <p>I have a <code>dict</code> that describes a mapping I want applied to every row in a CSV file.</p>
  
  <pre><code>dict1 = {"key1":["value1", "value2"], "key2":["value3"]}
  </code></pre>
  
  <p>My program <em>should read</em> one row and map the key in a specific column to the value(s) provided by the <code>dict</code>. If there's only one value per key, then the script should write to a new file the row containing the new value. If there are multiple values to a key, then there should be one new row written per value.</p>
  
  <p>For example, <code>csvin</code> contains 2 rows. One row has a column in which <code>key1</code> is present, and the other has <code>key2</code>. In this case, the output file <code>csvout</code> should contain <em>more</em> rows than <code>csvin</code>, in effect 3. Two of the rows (associated with <code>key1</code>) will be identical <em>except for one single value</em>.</p>
  
  <p>My current script is this:</p>
  
  <pre><code>def convSan(sfin, cfout):
      with open(sfin, "rb") as fin:
          with open(cfout, "wb") as fout:
              csvin = csv.reader(fin)
              csvout = csv.writer(fout, delimiter=",")
              fline = csvin.next()
              csvout.writerow(fline)
  
          for row in csvin:
              row[25] = dict1[row[25]]
              csvout.writerow(row)
  </code></pre>
  
  <p>This produces an output file with the same number of columns as the input file, but populates every field with the correct new values (some fields are now lists of values).</p>
  
  <p>The answer provided by @sr2222 works in the case of simple lists, but I cannot get it to work in my particular case.</p>
  
  <p>Help is appreciated.</p>