码迷,mamicode.com
首页 > 编程语言 > 详细

python读取excel,数字都是浮点型,日期格式是数字的解决办法

时间:2017-06-23 13:08:31      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:exce   import   cell   bsp   .com   for   encoding   com   str   

excel文件内容:

技术分享

 

读取excel:

# coding=utf-8
import xlrd
import sys

reload(sys)
sys.setdefaultencoding(utf-8)
import traceback


class excelHandle:
    def decode(self, filename, sheetname):
        try:
            filename = filename.decode(utf-8)
            sheetname = sheetname.decode(utf-8)
        except Exception:
            print traceback.print_exc()
        return filename, sheetname

    def read_excel(self, filename, sheetname):
        filename, sheetname = self.decode(filename, sheetname)
        rbook = xlrd.open_workbook(filename)
        sheet = rbook.sheet_by_name(sheetname)
        rows = sheet.nrows
        cols = sheet.ncols
        all_content = []
        for i in range(rows):
            row_content = []
            for j in range(cols):
                ctype = sheet.cell(i, j).ctype
                cell = sheet.cell_value(i, j)
                row_content.append(cell)
            all_content.append(row_content)
            print [ + ,.join("" + str(element) + "" for element in row_content) + ]
        return all_content


if __name__ == __main__:
    eh = excelHandle()
    filename = rG:\test\ctype.xls
    sheetname = Sheet1
    eh.read_excel(filename, sheetname)

输出:

[整形,175.0]
[字符串,最后的骑士]
[浮点型,6.23]
[日期,42909.6461574]
[空值,‘‘]

可以看到,数字一律按浮点型输出,日期却输出成一串数字?!!

 

python读取excel,数字都是浮点型,日期格式是数字的解决办法

标签:exce   import   cell   bsp   .com   for   encoding   com   str   

原文地址:http://www.cnblogs.com/xxiong1031/p/7069006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!