简体   繁体   English

我无法在 FastAPI 中读取上传的 csv 文件

[英]I can not read uploaded csv file in FastAPI

I am trying to iterate through a csv file.我正在尝试遍历 csv 文件。 However, the received file is giving a hard time to read.但是,接收到的文件很难阅读。 I searched for this, I could not get a clear solution!我搜索了这个,我找不到明确的解决方案!

@app.get("/uploadsequence/")
async def upload_sequence_form():
    return HTMLResponse("""
            <!DOCTYPE html>
            <html>
                <head>
                    <title>sequence upload</title>
                </head>
                <body>
                    <h1>upload sequence .CSV file</h1>
                    <form method='post' action='/uploadsequence/' enctype='multipart/form-data'>
                        Upload a csv file: <input type='file' name='csv_file'>
                        <input type='submit' value='Upload'>
                    </form>
                </body>
            </html>
            """)

@app.post("/uploadsequence/")
async def upload_sequence(csv_file: UploadFile = File(...), db = Depends(get_db)):
        csv_file_encoded = TextIOWrapper(csv_file.file, encoding='utf-8')
        csv_reader = csv.DictReader(csv_file_encoded)
        for row in csv_reader:
            if row["Well Type"] in ["RIG MAINTENANCE","RIG MOVE","RIG COMMISSIONING","ABANDONMENT","LEARNINGCURVE"]:
                crud.insert_sequence_record(db=db, row=row,is_drilling=False)
            else:
                crud.insert_sequence_record(db=db, row=row,is_drilling=True)

It gives me this error: csv_file_encoded = TextIOWrapper(csv_file.file, encoding='utf-8') AttributeError: 'SpooledTemporaryFile' object has no attribute 'readable'它给了我这个错误: csv_file_encoded = TextIOWrapper(csv_file.file, encoding='utf-8') AttributeError: 'SpooledTemporaryFile' object has no attribute 'readable'

I changed UploadFile to bytes :我将UploadFile更改为bytes

@app.post("/uploadsequence/")
async def upload_sequence(csv_file: bytes = File(...), db = Depends(get_db)):
        csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8')
        csv_reader = csv.DictReader(csv_file_encoded)
        for row in csv_reader:
            if row["Well Type"] in ["RIG MAINTENANCE","RIG MOVE","RIG COMMISSIONING","ABANDONMENT","LEARNINGCURVE"]:
                crud.insert_sequence_record(db=db, row=row,is_drilling=False)
            else:
                crud.insert_sequence_record(db=db, row=row,is_drilling=True)

It gives this error: csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8') AttributeError: 'bytes' object has no attribute 'readable'它给出了这个错误: csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8') AttributeError: 'bytes' object has no attribute 'readable'

I got rid of encoding:我摆脱了编码:

@app.post("/uploadsequence/")
async def upload_sequence(csv_file: bytes = File(...), db = Depends(get_db)):
        # csv_file_encoded = TextIOWrapper(csv_file, encoding='utf-8')
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            if row["Well Type"] in ["RIG MAINTENANCE","RIG MOVE","RIG COMMISSIONING","ABANDONMENT","LEARNINGCURVE"]:
                crud.insert_sequence_record(db=db, row=row,is_drilling=False)
            else:
                crud.insert_sequence_record(db=db, row=row,is_drilling=True)

It gives this error: self._fieldnames = next(self.reader) _csv.Error: iterator should return strings, not int (did you open the file in text mode?)它给出了这个错误: self._fieldnames = next(self.reader) _csv.Error: iterator should return strings, not int (did you open the file in text mode?)

I used the following to read the csv file.我使用以下内容来读取 csv 文件。 codecs.iterdecode worked for me. codecs.iterdecode为我工作。

csv_reader = csv.reader(codecs.iterdecode(csv_file.file,'utf-8'))
@app.post("/submitform")
async def handle_form(assignment_file: UploadFile = File(...)):
   print(assignment_file.filename)
   csv_reader = pd.read_csv(assignment_file.file)
   
   print(csv_reader)
   //else return response
   // csv data will be stored in the csv_reader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM