I am trying to get files as CSV or XLSX from the user and convert it to YAML. I have already made python scripts to convert the files from CSV to YAML and XLSX to YAML locally but I can't do it by taking the file from user. how can i read the file which gets uploaded?
@app.post("/cqa/uploadFile")
def create_upload_file(file: UploadFile = File(...)):
if file.filename.endswith('.csv') or file.filename.endswith('.xlsx'):
toYAML(file)
return {"filename": file.filename}
else:
raise HTTPException( status_code=status.HTTP_405_METHOD_NOT_ALLOWED)
def toYAML(file):
if file.filename.endswith('.csv'):
convert_yaml(codecs.iterdecode(file.file,'utf-8'))
else:
xlsx_to_csv(file)
convert_yaml("xlsxCSV.csv")
This is how I am converting to XLSX to CSV:
def xlsx_to_csv(file):
data_xls = pd.read_excel(file, index_col=None)
data_xls.to_csv("xlsxCSV.csv", encoding='utf-8', index=False)
This is how I convert CSV to YAML:
def convert_yaml(file):
file=open(file)
x = csv.reader(file)
file_arr=[]
for r in x :
file_arr.append(r)