title: Uploading a csv file with a fixed format
body: <p>I have a .csv file which my users have to download, input some data and upload to my site.</p>
  
  <p>Is there a better way of ensuring the data gets uploaded successfully based on my snippet below? What else should I be checking for? Would using a dialect be better?</p>
  
  <pre><code>def import(resident_file):
  
      try:
          file = resident_file.file.path
          reader = csv.reader(open(file, 'rU'), delimiter=',', quotechar='"')
          headerline = reader.next()
  
          for row in reader:
              try:
                  # do stuff
  
              except Exception, e:
                  print e
  
      except Exception, e:
          print e
  </code></pre>
  
  <p>An example of a problem I am running into is that when a user opens the file, inputs data and saves it, the delimiters change from <code>,</code> to <code>;</code>. How can I cover the various types of delimiters that the document could be saved in due to it being open in different programmes e.g excel in windows, excel in mac, open office in mac, open office in linux etc</p>
  
  <p>Another example of a problem is when the user tries to copy and paste the data into the template provided, all hell breaks loose.</p>
  
  <p><strong>UPDATE</strong>
  I'm using the <code>Sniffer</code> class now as mentioned in one of the answers below but its still not fool proof. </p>
  
  <p><strong>UPDATED CODE SNIPPET</strong></p>
  
  <pre><code>def bulk_import_residents(condo, resident_file):
  
      """
      COL 1       COL 2       COL 3           COL 4           COL 5        
      first_name  last_name   contact_number  unit_number     block_number
  
      """
  
      file_path = resident_file.file.path
      csvfile = open(file_path, 'rb')
      dialect =  csv.Sniffer().sniff(csvfile.read(1024))
      csvfile.seek(0)
      reader = csv.reader(csvfile, dialect)
      headerline = reader.next()
  
      for row in reader:
          try:
              data = ResidentImportData()
              data.condo = condo
              data.file = resident_file
              data.first_name = row[0]
              data.last_name = row[1] 
              data.contact_number = row[2]
              data.unit_number = row[3]
              data.block_number = row[4]
              data.save()
          except Exception, e:
              print '{0}'.format(e)
              raise Http404('Wrong template format')
  </code></pre>