Home  >  Q&A  >  body text

python - POST上传的excel(xls)文件,如何直接读进pandas,避免写入到磁盘?

怪我咯怪我咯2742 days ago983

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:40:44

    I think stringIO can solve your problem. Use it to save the file.stream content first. Passing to pd.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:40:44

    .xlsIt is not a text file, and setting the encoding should be invalid.
    Hold on and wait until the file is downloaded before pd.read_excel()


    read_excel(io) io)
    io可以是 url、ftp 之类的资源定位符。
    如果你知道.xlsio can be a resource locator such as url or ftp.

    If you know the location
    , you can open it directly.

    >>>help(pd.read_excel)
    Help on function read_excel in module pandas.io.excel:
    
    read_excel(io, …………)
        Read an Excel table into a pandas DataFrame
        
        Parameters
        ----------
        io : string, path object (pathlib.Path or py._path.local.LocalPath),
            file-like object, pandas ExcelFile, or xlrd workbook.
            The string could be a URL. Valid URL schemes include http, ftp, s3,
            and file. For file URLs, a host is expected. For instance, a local
            file could be file://localhost/path/to/workbook.xlsx

    🎜Try it, directly pd.read_excel(file) or (file.stream)🎜
    file = request.files['files']
    taxidata_from_excel = pd.read_excel(file) # (file.stream)

    reply
    0
  • Cancelreply