Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Appengine上使用xlrd读取excel文件_Python_Google App Engine_Flask_Xlrd - Fatal编程技术网

Python 如何在Appengine上使用xlrd读取excel文件

Python 如何在Appengine上使用xlrd读取excel文件,python,google-app-engine,flask,xlrd,Python,Google App Engine,Flask,Xlrd,我在appengine中使用xlrd。我用烧瓶 我无法读取输入文件,它会继续显示相同的错误消息 代码是 def read_rows(inputfile): rows = [] wb = xlrd.open_workbook(inputfile) sh = wb.sheet_by_index(0) for rownum in range(sh.nrows): rows.append(sh.row_values(rownum)) return

我在appengine中使用xlrd。我用烧瓶

我无法读取输入文件,它会继续显示相同的错误消息

代码是

def read_rows(inputfile):
    rows = []
    wb = xlrd.open_workbook(inputfile)
    sh = wb.sheet_by_index(0)
    for rownum in range(sh.nrows):
        rows.append(sh.row_values(rownum))
    return rows

@app.route('/process_input/',methods=['POST','GET'])
def process_input():
  inputfile = request.files['file']
  rows=read_rows(request.files['file'])
  payload = json.dumps(dict(rows=rows))
  return payload
我意识到这可能是因为没有上传并保存为文件。有解决办法吗?这也将帮助许多其他人。谢谢您的帮助,谢谢


更新:找到了我在下面发布的解决方案。对于那些对使用xlrd感到困惑的人,可以参考我发布的开源项目repo。密钥传递的是文件的内容,而不是文件名,从您的回溯判断

File "/Users/fauzanerichemmerling/Desktop/GAEHxl/gae/lib/xlrd/init.py", line 941, in biff2_8_load
    f = open(filename, open_mode)
您可以尝试将此行更改为:

f = filename
使用:


调用
open\u工作簿
的方式要求传递的是文件名,而不是包装实际文件的Flask
FileStorage
对象。

最终找到解决方案

我是这样做的。我没有保存文件,而是读取文件内容,让xlrd读取

def read_rows(inputfile):
  rows = []
  wb = xlrd.open_workbook(file_contents=inputfile.read())
  sh = wb.sheet_by_index(0)
  for rownum in range(sh.nrows):
    rows.append(sh.row_values(rownum))
  return rows
运行良好,将excel文件转换为可支持JSON的格式。如果要输出json,只需使用json.dumps()

完整的代码示例可以在上找到,它提供了xlrd的完整实现以及如何处理数据


Thx for the pointers

该代码位于第三方库中,只是随意更改它以接受文件而不是文件名似乎可能会让事情变得糟糕。这确实帮助了我。我需要获取内容而不是文件名。这样就产生了inputfile.read()。Thx很多
def read_rows(inputfile):
  rows = []
  wb = xlrd.open_workbook(file_contents=inputfile.read())
  sh = wb.sheet_by_index(0)
  for rownum in range(sh.nrows):
    rows.append(sh.row_values(rownum))
  return rows