python 读取excel 并将对应生成浮点数据进行处理

1.python 读取excel 检简单 

def open_excel(file=u'D:\\python27File\\crmAutoTest\\configFile\\logincase.xlsx'):
#     try:
#         data = xlrd.open_workbook(file)
#         return data
#     except Exception as e:
#         print(str(e))
# def excel_table_byname(file= u'D:\\python27File\\crmAutoTest\\configFile\\logincase.xlsx',colnameindex=0,by_name=u'Sheet1'):#修改自己路径
#      data = open_excel(file)
#      table = data.sheet_by_name(by_name) #获得表格
#      nrows = table.nrows  #拿到总共行数
#      colnames = table.row_values(colnameindex)  #某一行数据 ['姓名', '用户名', '联系方式', '密码']
#      list = []
#      for rownum in range(1, nrows): #也就是从Excel第三行开始,第一行表头不算
#          row = table.row_values(rownum)
#          if row:
#              app = {}
#              for i in range(len(colnames)):
#                  app[colnames[i]] = row[i]    #表头与数据对应
#              list.append(app)
#      return list

2.对应是文本数据 读出来是浮点或者科学计数 以及时间格式显示不准确

import xlrd
import traceback
from datetime import datetime
from xlrd import xldate_as_tuple

class excelHandle:
    def decode(self, filenam, sheetnam):
        try:
            filename = filenam.encode(encoding='utf8')
            sheetname = sheetnam.encode(encoding='utf8')
        except Exception:
            print( traceback.print_exc())
        return filenam, sheetnam
    def read_excel(self, filename, sheetname):
        filenam, sheetnam = self.decode(filename,sheetname)
        rbook = xlrd.open_workbook(filenam)
        sheet = rbook.sheet_by_name(sheetnam)
        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()
    filenam = r'D:\\python27File\\crmAutoTest\\configFile\\logincase.xlsx'
    sheetnam = 'Sheet1'
    eh.read_excel(filenam,sheetnam)
这就是对数据处理后 不会生成浮点数据格式的数据

你可能感兴趣的:(python,浮点数据,python,excel)