1

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

8
  • try this pd.DataFrame = pd.read_csv(file.file, delim_whitespace=True) Commented Jul 31, 2020 at 19:38
  • Tried did not worked Commented Jul 31, 2020 at 19:42
  • what does your CSV look like? Commented Jul 31, 2020 at 23:39
  • INSURANCE NAME,PATIENT NAME,INSURANCE ID, DATE OF BIRTH,CPT, DATE OF SERVICE COMMERCIAL,lastname,firstname,123,06/10/1965,xyz,05/10/2020 Commented Aug 1, 2020 at 5:38
  • Could it be that you are getting the error due to relative paths that differ between the server and your app? Also, are you running the app with docker or bare metal? Commented Aug 1, 2020 at 12:39

2 Answers 2

2

you can read the csv as following:

from io import BytesIO
df = pd.read_csv(BytesIO(file.file.read()))
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.