5

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!

@app.get("/uploadsequence/")
async def upload_sequence_form():
    return HTMLResponse("""
            <!DOCTYPE html>
            <html>
                <head>
                    <title>sequence upload</title>
                </head>
                <body>
                    <h1>upload sequence .CSV file</h1>
                    <form method='post' action='/uploadsequence/' enctype='multipart/form-data'>
                        Upload a csv file: <input type='file' name='csv_file'>
                        <input type='submit' value='Upload'>
                    </form>
                </body>
            </html>
            """)

@app.post("/uploadsequence/")
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["Well Type"] in ["RIG MAINTENANCE","RIG MOVE","RIG COMMISSIONING","ABANDONMENT","LEARNINGCURVE"]:
                crud.insert_sequence_record(db=db, row=row,is_drilling=False)
            else:
                crud.insert_sequence_record(db=db, row=row,is_drilling=True)

It gives me this error: csv_file_encoded = TextIOWrapper(csv_file.file, encoding='utf-8') AttributeError: 'SpooledTemporaryFile' object has no attribute 'readable'

I changed UploadFile to bytes:

@app.post("/uploadsequence/")
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["Well Type"] in ["RIG MAINTENANCE","RIG MOVE","RIG COMMISSIONING","ABANDONMENT","LEARNINGCURVE"]:
                crud.insert_sequence_record(db=db, row=row,is_drilling=False)
            else:
                crud.insert_sequence_record(db=db, row=row,is_drilling=True)

It gives this error: csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8') AttributeError: 'bytes' object has no attribute 'readable'

I got rid of encoding:

@app.post("/uploadsequence/")
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["Well Type"] in ["RIG MAINTENANCE","RIG MOVE","RIG COMMISSIONING","ABANDONMENT","LEARNINGCURVE"]:
                crud.insert_sequence_record(db=db, row=row,is_drilling=False)
            else:
                crud.insert_sequence_record(db=db, row=row,is_drilling=True)

It gives this error: self._fieldnames = next(self.reader) _csv.Error: iterator should return strings, not int (did you open the file in text mode?)

1
  • In the first case you simply have a File object that you can use as a classic file. In the second case, you have the raw bytes. Thus you can't open a file from the file you already have nor by passing the bytes. You could try using the DictReader on the spooled file, instead of the bytes Commented Sep 13, 2020 at 9:43

2 Answers 2

8

I used the following to read the csv file. codecs.iterdecode worked for me.

csv_reader = csv.reader(codecs.iterdecode(csv_file.file,'utf-8'))
Sign up to request clarification or add additional context in comments.

2 Comments

@Sören It is a standard python library
Future readers may want to have a look at this answer as well.
1
@app.post("/submitform")
async def handle_form(assignment_file: UploadFile = File(...)):
   print(assignment_file.filename)
   csv_reader = pd.read_csv(assignment_file.file)
   
   print(csv_reader)
   //else return response
   // csv data will be stored in the csv_reader

2 Comments

I think this is method using pandas will be slower than using the standard libraries
How do you do this without uploading CSV, but reading csv off a path directly from Python?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.