阅读python webapp2中excel文件的内容 [英] Reading contents of excel file in python webapp2

查看:91
本文介绍了阅读python webapp2中excel文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,即sample.csv和sample.xlsx,所有这些文件都存储在blobstore中。我可以使用以下代码读取csv文件的记录(位于blobstore中)

  blobReader = blobstore.BlobReader(blob_key)
inputFile = BlobIterator(blobReader)
如果inputFile是None:
values = None
else:
try:
stringReader = csv.reader(inputFile)
data = []
columnHeaders = []
for rowIndex,如果(rowIndex == 0):
columnHeaders = row
else:
data.append(row)
values = {'除了:
values = None

self.response.write(values)
>

sample.csv文件的上述代码的输出是

  {'的columnHeader s':['First Name','Last Name','Email','Mobile'],'data':[[''fx1','lx2','flx1x2@xxx.com','xxx-xxx- '''','''','','' zzz-zzzz']]} 

使用xlrd包,我可以读取excel文件内容,但在这里我必须指定确切的文件位置

$ $ $ $ $ $ $> book = xlrd.open_workbook('D:/sample.xlsx' )
first_sheet = book.sheet_by_index(0)
self.response.write(first_sheet.row_values(0))
cell = first_sheet.cell(0,0)
self。 response.write(cell.value)

有没有办法从blobstore读取excel文件的内容,我已经用下面的代码试过了:

  blobReader = blobstore.BlobReader(blobKey)
uploadedFile = BlobIterator(blobReader )
book = xlrd.open_workbook(fi le_contents = uploadedFile)
(或)
book = xlrd.open_workbook(file_contents = blobReader)

但它引发一些错误TypeError:'BlobReader'对象没有属性' getitem '。

有什么想法?谢谢..

解决方案

展望 doc for xlrd package doc中的open_workbook,看起来当你传递file_contents时,它期待一个字符串



然后,您需要考虑将Blob转换为字符串,可以使用 BlobReader.read(),它为您提供一串读取数据。


I have two files namely sample.csv and sample.xlsx, all those files are stored in blobstore.I am able to read the records of csv file(which is in the blobstore) using the following code

   blobReader = blobstore.BlobReader(blob_key)
   inputFile = BlobIterator(blobReader)
   if inputFile is None:
      values = None
   else:
      try:
         stringReader = csv.reader(inputFile)
         data = []
         columnHeaders = []
         for rowIndex, row in enumerate(stringReader):
            if(rowIndex == 0):
               columnHeaders = row
            else:
               data.append(row)
         values = {'columnHeaders' : columnHeaders, 'data' : data}
      except:
         values = None

      self.response.write(values) 

The output of the above code of a sample.csv file is

{'columnHeaders': ['First Name', 'Last Name', 'Email', 'Mobile'], 'data': [['fx1', 'lx2', 'flx1x2@xxx.com', 'xxx-xxx-xxxx'], ['fy1', 'ly2', 'fly1y2@yyy.com', 'yyy-yyy-yyyy'], ['fz1', 'lz2', 'flz1z2@zzz.com', 'zzz-zzz-zzzz']]}   

Using the xlrd package, i am able to read the excel file contents, but in this i have to specify the exact file location

   book = xlrd.open_workbook('D:/sample.xlsx') 
   first_sheet = book.sheet_by_index(0)
   self.response.write(first_sheet.row_values(0)) 
   cell = first_sheet.cell(0,0) 
   self.response.write(cell.value) 

Is there any way to read the excel file contents from the blobstore, i have tried it with the following code

   blobReader = blobstore.BlobReader(blobKey)
   uploadedFile = BlobIterator(blobReader) 
   book = xlrd.open_workbook(file_contents=uploadedFile)
                    (or)
   book = xlrd.open_workbook(file_contents=blobReader)

But it throws some error TypeError: 'BlobReader' object has no attribute 'getitem'.

Any ideas? Thanks..

解决方案

Looking into the doc for open_workbook in the xlrd package doc, it seems that when you pass "file_contents", it's expecting a string.

Then you need to look into turning a Blob into a String, which can be done with BlobReader.read(), which gives you a string of the read data.

这篇关于阅读python webapp2中excel文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆