title: I can not read uploaded csv file in FastAPI
body: <p>I am trying to iterate through a csv file. However, the received file is giving a hard time to read. I searched for this, I could not get a clear solution!</p>
  <pre><code>@app.get(&quot;/uploadsequence/&quot;)
  async def upload_sequence_form():
      return HTMLResponse(&quot;&quot;&quot;
              &lt;!DOCTYPE html&gt;
              &lt;html&gt;
                  &lt;head&gt;
                      &lt;title&gt;sequence upload&lt;/title&gt;
                  &lt;/head&gt;
                  &lt;body&gt;
                      &lt;h1&gt;upload sequence .CSV file&lt;/h1&gt;
                      &lt;form method='post' action='/uploadsequence/' enctype='multipart/form-data'&gt;
                          Upload a csv file: &lt;input type='file' name='csv_file'&gt;
                          &lt;input type='submit' value='Upload'&gt;
                      &lt;/form&gt;
                  &lt;/body&gt;
              &lt;/html&gt;
              &quot;&quot;&quot;)
  
  @app.post(&quot;/uploadsequence/&quot;)
  async def upload_sequence(csv_file: UploadFile = File(...), db = Depends(get_db)):
          csv_file_encoded = TextIOWrapper(csv_file.file, encoding='utf-8')
          csv_reader = csv.DictReader(csv_file_encoded)
          for row in csv_reader:
              if row[&quot;Well Type&quot;] in [&quot;RIG MAINTENANCE&quot;,&quot;RIG MOVE&quot;,&quot;RIG COMMISSIONING&quot;,&quot;ABANDONMENT&quot;,&quot;LEARNINGCURVE&quot;]:
                  crud.insert_sequence_record(db=db, row=row,is_drilling=False)
              else:
                  crud.insert_sequence_record(db=db, row=row,is_drilling=True)
  </code></pre>
  <p>It gives me this error:
  <code>csv_file_encoded = TextIOWrapper(csv_file.file, encoding='utf-8') AttributeError: 'SpooledTemporaryFile' object has no attribute 'readable'</code></p>
  <p>I changed <code>UploadFile</code> to <code>bytes</code>:</p>
  <pre><code>@app.post(&quot;/uploadsequence/&quot;)
  async def upload_sequence(csv_file: bytes = File(...), db = Depends(get_db)):
          csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8')
          csv_reader = csv.DictReader(csv_file_encoded)
          for row in csv_reader:
              if row[&quot;Well Type&quot;] in [&quot;RIG MAINTENANCE&quot;,&quot;RIG MOVE&quot;,&quot;RIG COMMISSIONING&quot;,&quot;ABANDONMENT&quot;,&quot;LEARNINGCURVE&quot;]:
                  crud.insert_sequence_record(db=db, row=row,is_drilling=False)
              else:
                  crud.insert_sequence_record(db=db, row=row,is_drilling=True)
  </code></pre>
  <p>It gives this error: <code>csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8') AttributeError: 'bytes' object has no attribute 'readable'</code></p>
  <p>I got rid of encoding:</p>
  <pre><code>@app.post(&quot;/uploadsequence/&quot;)
  async def upload_sequence(csv_file: bytes = File(...), db = Depends(get_db)):
          # csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8')
          csv_reader = csv.DictReader(csv_file)
          for row in csv_reader:
              if row[&quot;Well Type&quot;] in [&quot;RIG MAINTENANCE&quot;,&quot;RIG MOVE&quot;,&quot;RIG COMMISSIONING&quot;,&quot;ABANDONMENT&quot;,&quot;LEARNINGCURVE&quot;]:
                  crud.insert_sequence_record(db=db, row=row,is_drilling=False)
              else:
                  crud.insert_sequence_record(db=db, row=row,is_drilling=True)
  </code></pre>
  <p>It gives this error:
  <code>self._fieldnames = next(self.reader) _csv.Error: iterator should return strings, not int (did you open the file in text mode?)</code></p>