[Python] How to upload a CSV file in FastAPI and convert it into JSON?

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to upload my .csv file to my FastAPI server, then convert it into JSON and return it to the client. However, when I try to process it directly (without storing it somewhere), I get this error:

    Error : FileNotFoundError: [Error 2] No such file or directory : "testdata.csv"


    This is my FastAPI code:

    async def upload(file: UploadFile = File(...)):
    data = {}
    with open(file.filename,encoding='utf-8') as csvf:
    csvReader = csv.DictReader(csvf)
    for rows in csvReader:
    key = rows['No']
    data[key] = rows
    return {data}```

    Continue reading...

Compartilhe esta Página