title: Iterate over and validate large uploaded CSV files in Django
body: <p>I'm using the Django module <a href="https://github.com/juliomalegria/django-chunked-upload" rel="nofollow noreferrer">django-chunked-upload</a> to receive potentially large CSV files. I can assume the CSVs are properly formatted, but I can't assume what the delimiter is. </p>
  
  <p>Upon completion of the upload, an <a href="https://docs.djangoproject.com/en/2.1/ref/files/uploads/" rel="nofollow noreferrer">UploadedFile</a> object is returned. I need to validate that the correct columns are included in the uploaded CSV and that the data types in each column are correct.</p>
  
  <p>loading the file with <code>csv.reader()</code> doesn't work:</p>
  
  <pre><code>reader = csv.reader(uploaded_file)
  next(reader)
  &gt;&gt;&gt; _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
  </code></pre>
  
  <p>This might be because <code>uploaded_file.content_type</code> and <code>uploaded_file.charset</code> are both coming through as <code>None</code>.</p>
  
  <p>I've come up with a fairly inelegant solution to grab the header and iterate over the rows:</p>
  
  <pre><code>i = 0
  header = ""
  for line in uploaded_file:
      if i == 0:
          header = line.decode('utf-8')
          header_list = list(csv.reader(StringIO(header)))
          print(header_list[0])
          #validate column names
      else:
          tiny_csv = StringIO(header + line.decode('utf-8'))
          reader = csv.DictReader(tiny_csv)
          print(next(reader))
          #validate column types
  </code></pre>
  
  <p>I also considered trying to load the path of the actual saved file:</p>
  
  <pre><code>path = #figure out the path of the temp file
  f = open(path,"r")
  reader = csv.reader(f)
  </code></pre>
  
  <p>But I wasn't able to get the temp file path from the UploadedFile object.</p>
  
  <p>Ideally I would like to create a normal reader or DictReader out of the UploadedFile object, but it seems to be eluding me. Anyone have any ideas?  - Thanks</p>