Home > Mobile >  Creating a dictionary from a csv file. I am taking the csv file as an input in an api endpoint in fa
Creating a dictionary from a csv file. I am taking the csv file as an input in an api endpoint in fa

Time:01-09

I basically want to take a CSV file as input, and return a dictionary as a response. Since the default fastapi UploadFile module returns a spooled temp file, which i can't use as an input to pandas.read_csv(), what i am trying to do is write the contents of the uploaded file to another file buffer, and than use read_csv() and to_dict() to convert the CSV data to a dictionary.

This is how i am trying to do it:

async def CSVToDict(file: UploadFile = File(...)):
    if file.filename.endswith('.csv'):
        #open a new file buffer to write the data to
        with open(file.filename, 'rb ') as temp_file:
            #read the contents of the uploaded file
            content = await file.read()
            #write the content to the new buffer
            temp_file.write(content)
            #convert the data to a dictionary
            dict = pandas.read_csv(temp_file, header=None, index_col=0, squeeze=True).to_dict()
            #delete the buffer as i don't want to store anything in the server
            temp_file.flush()
            #return the dictionary as data
            return {"data": dict}
    else:
        return {"Invalid file format"}

but i am getting the following error:

pandas.errors.EmptyDataError: No columns to parse from file

I understand that this is because the file i am creating "as temp_file" is not created as a CSV file and read_csv() requires a CSV file to work. But i can't figure out a way to correct this. How do i use pandas to convert an uploaded csv file to a dictionary??

Thank you for your time... And wishing all a happy new year!!

CodePudding user response:

I resolved the issue, instead of writing data to a new file, we can indeed encode the data to UTF-8 instead of reading bytes.

Using the default CSV module, we can pass the file after decoding it:

iterator = csv.reader(codecs.iterdecode(data.file, 'utf-8'), delimiter=',')

This "iterator" can now be used as a CSV reader.

Read more about codecs for more details.

  •  Tags:  
  • Related