Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can not read uploaded csv file in FastAPI

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?)

like image 240
Sami Al-Subhi Avatar asked Jan 24 '23 18:01

Sami Al-Subhi


2 Answers

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'))
like image 64
Sami Al-Subhi Avatar answered Jan 27 '23 06:01

Sami Al-Subhi


@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
like image 26
sangaraju Lokesh varma Avatar answered Jan 27 '23 06:01

sangaraju Lokesh varma