你如何阅读Appengine上的xlrd

你如何阅读Appengine上的xlrd

本文介绍了你如何阅读Appengine上的xlrd excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我无法读取输入文件,它一直显示相同的错误信息



代码是

$ $ p $ $ code $ def read_rows(输入文件):
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))
返回有效载荷

我意识到这可能是由于没有上传并将其另存为文件引起的。任何解决方法?这也可以帮助很多人。任何帮助表示赞赏,thx



更新:找到我在下面发布的解决方案。对于那些用xlrd搞糊涂的可以参考我贴的开源项目仓库。关键是传递文件的内容而不是文件名

解决方案

最后找到一个解决方案

这是我如何做到的。而不是保存文件,我读取文件的内容,让xlrd读取它。

  def read_rows(inputfile):$ b ($)$ b $ = 

$
rows.append(sh.row_values(rownum))
return rows

很好地工作,并将Excel文件转换成JSON格式。如果您想输出json,只需使用json.dumps()。

完整的代码示例可以在,它具有完整的实现的xlrd和如何处理数据。

Thx为指针


I am using xlrd in appengine. I use flask

I cant read the input file and it keeps on showing the same error message

the code is

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

I realize that this might be caused by not uploading and saving it as a file. Any workaround on this? This would help many others as well. Any help is appreciated, thx

Update: Found a solution that I posted below. For those confused with using xlrd can refer to the open source project repo I posted. The key is passing the content of the file instead of the filename

解决方案

Find a solution finally

here's how I do it. Instead of saving the file, I read the content of the file and let xlrd reads it

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

worked nicely and turned the excel files into JSON-able formats. If you want to output the json simply use json.dumps().

full code example can be found at https://github.com/cjhendrix/HXLator/blob/master/gae/main.py and it features full implementation of the xlrd and how to work with the data.

Thx for the pointers

这篇关于你如何阅读Appengine上的xlrd excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:48