Reputation: 343
I have written FastAPI to upload a file but in my new requirement, I have to create CLI to upload a file for that I am calling route function.
@app.post("/uploadfile/")
async def create_upload_file(file: Uploadfile =File(...));
pd.DataFrame = pd.read_csv(file.file)
return True
create_upload_file(file=UploadFile(input_file_path))
when i tried to read that file like : claims: pd.DataFrame = pd.read_csv(file.file) i am getting following error
No columns to parse from file
Upvotes: 1
Views: 630
Reputation: 41
You can try reading and decoding the content from the file and then using pandas to read csv.
from io import StringIO
import pandas as pd
@app.post("/uploadfile/")
async def create_upload_file(file: Uploadfile =File(...)):
file = StringIO((await file.read()).decode("utf-8"))
df = pd.read_csv(file)
return True
Upvotes: 0
Reputation: 61
you can read the csv as following:
from io import BytesIO
df = pd.read_csv(BytesIO(file.file.read()))
Upvotes: 2