标签: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 = r‘G:\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