title: Reading and Writing CSV files in Python
body: <p>I am trying to write the first 3 rows of Vehicle Owners.csv onto Speeding Owners.csv along with the 3rd and 4th line of Speeding Vehicles.csv. However, if there is no speeding vehicle to match the Vehicle Owners, the Vehicle Owners go to missing owners.csv</p>
  
  <p>Can someone please explain why this is not working?</p>
  
  <p>Speeding Vehicles.csv looks like this:</p>
  
  <blockquote>
    <p>GH76GHJ,wallace Street,30,35.0<br>
    GH67MNB,wallace Street,30,35.0<br>
    GH76GHJ,green street,40,55.9<br>
    GH76GJ,green street,40,55.9</p>
  </blockquote>
  
  <p>Vehicle Owners.csv looks like this:</p>
  
  <blockquote>
    <p>GH76HJK,1 red street,ross gamble<br>
    BH98LOP,1 kings road,muammar gaddafi<br>
    CX87YHJ,1 Alison drive,chandler bing<br>
    HJ09UJI,6 avenue,idi amin<br>
    KI59TOM,7 wall street,robert mugabe</p>
  </blockquote>
  
  <p>My current code is:</p>
  
  <pre><code>import csv
  
  with open('Vehicle Owners.csv', 'r') as vehicleownersfile:
      owner_reader = csv.reader(vehicleownersfile)
      details = {row[0].lower(): row for row in owner_reader}
  
  with open('Speeding Vehicles.csv', 'r') as speedingvehiclesfile:
      owner_reader = csv.reader(speedingvehiclesfile)
  
      with open('Speeding Owners.csv', 'w') as speedingownersfile:
          sowriter = csv.writer(speedingownersfile)
  
          with open('Missing Owners.csv', 'w') as missingownersfile:
              mowriter = csv.writer(missingownersfile)
  
              for row in owner_reader:
                  detail = details.get(row[1].lower())
  
                  if detail is None:
                       mowriter.writerow(row[0], row[1], row[2])
                  else:
                       sowriter.writerow(row[0], row[1], row[2], detail[1], detail[2])
  </code></pre>