Python- Read excel with xlrd module, are floating-point numbers, date format digital solutions

excel file content:

 

Read excel:

Copy the code
# 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):
                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)
Copy the code

Output:

[ 'Shaping', '175.0'] 
[ 'string', 'Last Knight'] 
[ 'float', '6.23'] 
[ 'date', '42,909.6461574'] 
[ 'null' ''] 
[ 'boolean', '1']

You can see, all according to figures floating point output, but the output into a string date decimals? ! Boolean 1 or 0 output

 

Code minor changes: the data type of the table to look

Copy the code
        for i in range(rows):
            row_content = []
            for j in range(cols):
                ctype = sheet.cell(i, j).ctype #表格的数据类型
                print ctype,
                cell = sheet.cell_value(i, j)
                row_content.append(cell)
            all_content.append(row_content)
            print
            print '[' + ','.join("'" + str(element) + "'" for element in row_content) + ']'
Copy the code

Output:

Copy the code
12 
[ 'shaping', '175.0'] 
1 1 
[ 'string', 'Last Knight'] 
12 
[ 'float', '6.23'] 
13 
[ 'date', '42,909.6461574'] 
1 0 
[ 'null', ''] 
14 
[ 'boolean', '1']
Copy the code

python excel in reading contents of a cell is returned into five types, i.e. in the above example ctype:


ctype: 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

 

So, determine what ctype, and then make the appropriate treatment on it.

 

The final code:

Copy the code
# coding=utf-8
import xlrd
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
import traceback
from datetime import datetime
from xlrd import xldate_as_tuple


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)
                if ctype == 2 and cell % 1 == 0:  # 如果是整形
                    cell = int(cell)
                elif ctype == 3:
                    # 转成datetime对象
                    date = datetime(*xldate_as_tuple(cell, 0))
                    cell = date.strftime('%Y/%d/%m %H:%M:%S')
                elif ctype == 4:
                    cell = True if cell == 1 else False
                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)
Copy the code

Output:

[ 'Shaping', '175'] 
[ 'string', 'the last knight'] 
[ 'float', '6.23'] 
[ 'date', '2017/23/06 15:30:28'] 
[ 'null' ''] 
[ 'boolean' 'True']

 

Guess you like

Origin www.cnblogs.com/yuanfang0903/p/11596804.html