반응형
빠른 속도로 파일을 업로드하는 방법API를 Panda Dataframe으로 변환하시겠습니까?
파일을 Fast에 업로드하고 싶습니다.API 백엔드를 사용하여 Pandas DataFrame으로 변환합니다. 하지만 Fast를 사용하는 방법을 이해하지 못하는 것 같습니다.API의 개체입니다. 좀 더 구체적으로 어떤 기능을 전달하면 될까요?
내 금식이야API 끝점:
@app.post("/upload")
async def upload_file(file: UploadFile):
df = pd.read_csv("")
print(df)
return {"filename": file.filename}
아래에는 업로드된 파일을 빠른 속도로 변환하는 방법에 대한 다양한 옵션이 있습니다.Panda 데이터 프레임에 대한 API입니다. DataFrame을 JSON으로 변환하여 클라이언트에게 반환하고 싶다면 을 보십시오. 대신 엔드포인트를 사용하고 싶다면 파일 내용을 읽는 방법과 사용법을 이해하시기 바랍니다. 또한 I/O 작업(아래 옵션에서 설명됨)을 블록으로 둘러싸서 가능한 모든 예외와 개체를 올바르게 포착/제시할 수 있도록 하는 것이 가장 좋습니다(및 참조).
옵션 1
는 객체를 수락할 수 있으므로 객체를 직접 전달할 수 있습니다. 속성을 사용하여 얻을 수 있는 실제 파이썬을 보여줍니다. 다음은 예를 제시하겠습니다. 참고: 따라서 엔드포인트를 사용하려는 경우 설명된 대로 방법을 사용하여 파일의 내용을 읽은 다음 아래 리메이닝 옵션 중 하나를 사용하여 내용을 전달하는 것이 좋습니다. 또는 Starlette(설명된 대로)를 사용하면 별도의 스레드에서 를 실행하여 기본 스레드가 차단되지 않도록 할 수 있습니다.
from fastapi import FastAPI, File, UploadFile
import pandas as pd
app = FastAPI()
@app.post("/upload")
def upload_file(file: UploadFile = File(...)):
df = pd.read_csv(file.file)
file.file.close()
return {"filename": file.filename}
옵션 2
바이트를 문자열로 변환한 다음 메모리 내 텍스트 버퍼(예: 데이터 프레임으로 변환할 수 있음)로 로드합니다.
from fastapi import FastAPI, File, UploadFile
import pandas as pd
from io import StringIO
app = FastAPI()
@app.post("/upload")
def upload_file(file: UploadFile = File(...)):
contents = file.file.read()
s = str(contents,'utf-8')
data = StringIO(s)
df = pd.read_csv(data)
data.close()
file.file.close()
return {"filename": file.filename}
옵션 3
대신 메모리 내 바이트 버퍼를 사용하십시오(예: 옵션 2:
from fastapi import FastAPI, File, UploadFile
import pandas as pd
from io import BytesIO
import uvicorn
app = FastAPI()
@app.post("/upload")
def upload_file(file: UploadFile = File(...)):
contents = file.file.read()
data = BytesIO(contents)
df = pd.read_csv(data)
data.close()
file.file.close()
return {"filename": file.filename}
반응형
'개발하자' 카테고리의 다른 글
python 데이터 클래스 __init_ 메서드에서 강제 형식 변환 (0) | 2022.11.16 |
---|---|
동적 쿼리 매개 변수 빠른 속도API (0) | 2022.11.16 |
how can we add project number from variable in terraform gcp resource iam binding (0) | 2022.11.14 |
Kubernetes 시크릿 볼륨 대 변수 (0) | 2022.11.14 |
Svelte: How to handle the custom writtable store's async init's promise in the component? (0) | 2022.11.14 |