用于tablib导入Excel

用于tablib导入Excel

本文介绍了Python - 用于tablib导入Excel(xls,xlsx)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定如何将Excel文件导入到我的Python脚本中。我只是几天到Python,所以我猜这是非常明显的我失踪了。我使用的是Python 3和tablib模块。从tablib站点的示例中,我已经找出了如何以xls格式保存文件

I'm having trouble figuring out how to import Excel files into my Python script. I'm only a few days into Python so I'm guessing it's something very obvious I'm missing. I'm using Python 3 and the tablib module. From the examples on the tablib site, I've worked out how to save files in xls format

    def saveXLS(self, name, data):
        # Form the dataset with the accompanying headers
        dataTab =  tablib.Dataset()
        dataTab.headers = data[0][:]

        for i in range(1,len(data)):
            dataTab.append(data[i][:])

        with open(self.saveDir + name + ".xls", 'wb') as f:
            f.write(dataTab.xls)

我知道这个循环是可怕的和非诡计的,但是当我们得到结果的时候很重要,因为它是为了工作)。目前,我打开Excel工作簿并将其保存为文本文件(我应该指出,我的所有数据都是制表符分隔的,包括字符串,甚至是数字)。

(I know that loop is horrible and un-Pythonic, but it's important I get results at the moment as it's for work). At the moment, I open the Excel workbook and save it as a text file (I should point out that all my data is tab-delimited and consists of strings, even for numbers).

我打开它这样

    def loadTxt(self,name, fileType, data):
            if( fileType == "txt"):
                with open(self.currentWorkingDir + "\\" + name + ".txt",'r') as f:
                    reader=csv.reader(f,delimiter='\t')
                    for X in reader:
                        data.append(X)

我尝试复制tablib网站上的dbf示例()获取

I tried copying the "dbf" example on the tablib website (http://tablib.readthedocs.org/en/latest/api/) to get

    def loadXLS(self):
            self.data = tablib.Dataset()
            self.data = open('Data.xlsx').read()
            return self.datav

我收到一个错误(正如我预期的,因为我从我的屁股拉它)

And I get an error (as I expected, as I pulled it from my ass)

UnicodeDecodeError:'charmap'编解码器无法解码位置637的字节0x8f:字符映射到。

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 637: character maps to .

我真的不知道如何计算不幸的是,所以任何建议将非常感激。

I really have no clue how to figure this out unfortunately, so any advice would be really appreciated.

推荐答案

你可能已经解决了现在,但对于下一个人,您需要将Excel文件读为二进制

You've probably figured it out by now, but for the next person, you need to read the Excel file as binary:

my_input_stream = open("my_file.xlsx", "rb")
my_dataset = tablib.import_set(my_input_stream)
dataset[1:5]

这篇关于Python - 用于tablib导入Excel(xls,xlsx)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 00:13